Felipe Pina
BAN USERI have 18 years as web developer. I'm currently assigned as a full stack computer engineer. I have 18 years in web application development and infrastructure.
I have been worked in big companies as consultant and formal employee, and i entrepreneur in my own company too.
I have solid knowledge in OO, TDD, Java, GO, JavaScript, Angular. I am a certified cloud solutions architect from AWS. I already work with Agile Scrum methodology in various teams with different projects, gaining experience with different technologies
Why the answer is not { 120, 125 } ?
{{
func main() {
arr := []int{90, 85, 75, 60, 120, 150, 125}
integer := 250
heap := generateHeapOfPairs(integer, arr)
fmt.Println("All pairs", heap.ToArray())
if !heap.IsEmpty() {
fmt.Println("Largest Pair", heap.RootElement().ToString())
}
}
func generateHeapOfPairs(limit int, arr []int) minMaxHeap.Heap {
length := len(arr)
pairs := make([]minMaxHeap.Comparable, 0, length*length)
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
p := Pair{arr[i], arr[j]}
if j > i && p.Sum() < limit {
pairs = append(pairs, p)
}
}
}
return minMaxHeap.NewMaxHeap(pairs)
}
}}
package t9keyboard
/**
You have a list of words with ranking.
Now you need to create a function that will take this list as input and provide a way so that a T9 keyboard
can provide three top results of probable words based on rankings for the numbers punched in.
Created by Felipe on 24/03/2015.
*/
class T9Keyboard extends Trie {
@Override
Node search(Object key) {
def node = super.search(key)
// if found
if (node) {
node.readData()
}
return node
}
/**
* Returns the first three suggestion based on the string passed.
*/
def suggestions(String word) {
Node node = search(word)
def suggestions = []
if (node && !node.isLeaf()) {
int i = 0;
def it = node.queue.iterator() // iterator on descending rank order
// for (int i = 0; i < 3 && i < node.children.size(); i++) {
while (it.hasNext() && i<3) {
Node suggestion = it.next();
suggestions << suggestion.suggestion
i++
}
}
suggestions
}
static void main(String[] args) {
def t = new T9Keyboard()
t.add("trie")
t.add("tree")
t.add("i")
t.add("it")
t.add("ite")
assertFound(t, "t")
assertFound(t, "tr")
assertFound(t,"tri")
assertFound(t,"trie")
assertFound(t,"tre")
assertFound(t,"i")
assertFound(t,"it")
assertFound(t,"ite")
assert null == t.search("triex")
assert null == t.search("treexx")
assert null == t.search("ix")
assert t.suggestions("tr") == ['trie','tree']
// increase tree rank
assertFound(t,"tre")
assertFound(t,"tre")
assert t.suggestions("tr") == ['tree','trie']
println "T9Keyboard test OK"
}
static def assertFound(def t, String key) {
assert t.search(key)?.data == key
}
}
package t9keyboard
/**
* Created by Felipe on 28/03/2015.
*/
class Trie {
/*root node with empty data*/
def root = new Node(data:'')
Node search(Object key) {
def node = root
def result = key.find {
node = node.children[it]
node == null
}
node
}
def add(key) {
def node = root
key.each {
/*find the relevant child node*/
def child = node.children[it]
/*if child does not exist, creates it*/
if (child == null) {
child = node.addChild(it)
}
node = child
}
node
}
}
package t9keyboard
import groovy.transform.EqualsAndHashCode
/**
* Created by Felipe on 24/03/2015.
*/
@EqualsAndHashCode(includes = ['data'])
class Node {
static Comparator rankComparator = [compare:{a,b> b.rank <=> a.rank }] as Comparator
// priority queue
def queue = new PriorityQueue<Node>(30, rankComparator)
/*data to be save*/
def data
/*child nodes of this node*/
def children = [:]
// frequency of data access
private int rank = 0
Node parent
def readData() {
// remove this node from parent's children
if (parent.queue.remove(this)) {
rank++
parent.queue.add( this )
}
data
}
def isLeaf (){
children.isEmpty()
}
def addChild(def key) {
def child = children[key] = new Node(data: (data + key), parent: this)
queue.add(child)
child
}
String getSuggestion() {
Node child = queue.peek()
if (child) {
child.suggestion
} else
data
}
}

Felipe Pina
March 29, 2015 import groovy.transform.EqualsAndHashCode
/**
Given a set of n people, find the celebrity.
Celebrity is a person who:
1. Knows only himself and no one else
2. Every one else knows this person
You are given the following helper function:
bool knows(i, j);
Returns:
True: If i knows j
False: otherwise
I proposed the O(n2) algorithm at first but he wanted me to improve on it. He wanted an O(n) algorithm
Ex: A>B>C
A>C<D
A>C means A knows C
C<A means C is Known for A
C is celebrity
* Created by Felipe on 21/03/2015.
*/
class Celebrity {
static class Person {
private String name
List<Person> knows = new ArrayList<>()
List<Person> isknown = new ArrayList<>()
// the ideal would be a structure that was a list and set together
//private static Set<Person> friendsSet = new TreeSet<Person>([compare:{a,b> b.knows.size() <=> a.knows.size() }] as Comparator)
private static Set<Person> friendsSet = new LinkedHashSet<Person>()
private static List<Person> friendsList = new ArrayList<>()
Person know(Person p) {
knows << p
p.isknown << this
addFriends(p)
addFriends(this)
p
}
Person isKnown(Person p) {
isknown << p
p.knows << this
addFriends(p)
addFriends(this)
p
}
private void addFriends(Person p) {
if (friendsSet.add( p )) {
friendsList.add( p )
}
}
boolean knows(i, j) {
if (i>=0 && j>=0 && i < friendsSet.size() && j < friendsList.get(i).knows.size()) {
friendsList.get(i).knows.get(j) != null
}
false
}
Person findCelebrity() {
// with java 7 using the TimSort Average case performance O(n log n)
//def cmp = [compare:{a,b> b.knows.size() <=> a.knows.size() }] as Comparator
friendsSet = friendsSet.sort {a,b> a.knows.size() <=> b.knows.size() }
friendsSet.find { Person p >
// Every one else knows this person
(p.isknown.size() == friendsSet.size() 1) && (p.knows.size() == 0) //Knows only himself and no one else
}
}
}
static void main (String[] args) {
def personA = new Person(name: "a")
def personC = new Person(name: "c")
def a = personA
.know(new Person(name:"b"))
.know(personC)
.isKnown(new Person(name:"d"))
personA.know(personC)
println "Celebrity is Person ${personA.findCelebrity()?.name}"
}
}

Felipe Pina
March 24, 2015 using Groovy way and recursion
class ListOrderNumbersFromMatrix {
def static evaluate(matriz) {
def minQueue = null
def min = null
matriz.each() { Queue row >
if (min == null)
min = row.peek()
if (row.peek() && row.peek() <= min)
minQueue = row;
}
println minQueue.poll();
if (!matriz.flatten().empty)
evaluate(matriz)
}
static void main(String[] args) {
def matriz = [
[20,40,80] as LinkedList,
[5,60,90] as LinkedList,
[45,50,55] as LinkedList
]
ListOrderNumbersFromMatrix.evaluate(matriz);
}
}
Open Chat in New Window
Solution 2  Why the answer is not { 120, 125 } ?
 Felipe Pina July 15, 2019