Google Interview Question
Software EngineersCountry: United States
I think create two maps, one is hashmap which is name -> rank, another is treemap, rank->list<String>(the names), then we can use treemap's properties to extract all of people who has same rank. Correct me if I am wrong! Any other idea?
This sounds right. Not sure if you need the second map. You can just update the entry for a given name, and let the treeMap update the entry's position in the tree. This should be a log(N) operation. Similarly, getting an entry for a given rank should be just navigating the tree to pick the appropriate node. In case of duplicated entries, we should probably return just the first match.
Assumptions:
1. Names are unique keys in a chart. The 'updateEntity' method should have been 'updateEntities' if a single name (as for signature of that method) may refer to more than one Entity.
2. The 'updateEntity' method is used to increment the score of 1 unit.
Given this, the design is really driven by the more often used operation. Let's say, if we need to do a lot of updates and rarely need to get_from_rank then the following solution is optimal. Viceversa, we could order and store the dictionary at every update_entity.
class ScorerChart:
def __init__(self, name, score):
self.chart = {name: score}
def add(self, name, score):
self.chart[name] = score
def update_entity(self, name):
self.chart[name] += 1 # O(1)
def get_entity_from_rank(self, rank):
sorted_chart = sorted(self.chart.items(), key=lambda e: e[1], reverse=True) # O(n log n)
return sorted_chart[rank - 1] # O(1)
if __name__ == "__main__":
c = ScorerChart("Carl", 74)
c.add("Alex", 55)
c.add("Isla", 40)
c.add("Tom", 5)
c.add("Michael", 32)
c.add("Nigel", 75)
c.add("Greg", 15)
assert c.get_entity_from_rank(1) == ("Nigel", 75)
c.update_entity("Carl")
c.update_entity("Carl")
assert c.get_entity_from_rank(1) == ("Carl", 76)
We can use a regular STL map to store the score for each player.
Next, we define a custom map using AVL tree and ll expose another operation to find the kth element in it. This operation can be implemented in O(log N) time since the AVL tree is always balanced. Link:
geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/
Simple Java Solution :
/*
* Click `Run` to execute the snippet below!
*/
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
// class to represent the required data structure
class Solution
{
public static void main(String[] args) {
// Create a HashMap and add some values
HashMap<String, Integer> map
= new HashMap<>();
map.put("carl", 70);
map.put("alex", 55);
map.put("isla", 40);
Integer value = 70;
for(Entry<String, Integer> entry: map.entrySet()) {
if(entry.getValue() == value) {
System.out.println("The key for value " + value + " is " + entry.getKey());
break;
}
}
// print map details
System.out.println("HashMap: "
+ map.toString());
// provide value for the key which has
// to replace it's current value,
// using replace(K key, V value) method
map.replace("alex", 65);
// print new mapping
System.out.println("New HashMap: "
+ map.toString());
}
}
are all of names different with each other in this list? Or, there are duplicate names in the list?
- lixx3527 July 09, 2019