Google Interview Question for Software Engineers


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

are all of names different with each other in this list? Or, there are duplicate names in the list?

- lixx3527 July 09, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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?

- lixx3527 July 09, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous July 29, 2019 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Rank is not a score. Rank is the index in the list sorted by the score.

- Evg February 21, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- nicolarusso March 03, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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/

- Rajat.nitp July 02, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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());
}
}

- Rakshinda Shaikh October 10, 2022 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More