Google Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

We can implement it by using a combination of HashMap and BST. HashMap will contain entry for key-value pair and BST only keys. All keys will be maintained in BST (sorted order).

lookup(Key): Make a simple lookup in to HashMap
rangeLookup() - It will be in two parts -
Keys[] rage(Key1, Key 2) from BST
Entry[] lookups(Keys[]) in HashMap

- Ajeet December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Why not go for B+ tree: en.wikipedia.org/wiki/B%2B_tree
Lookup is O(log n)
Range lookup is O(log n + (Key2 - Key1))

- Neeraj December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Since lookup is the major concern here, BSTs are the best. However, there is a form of BST called the Splay Tree, which are efficient for lookups. They have an interesting property which makes them bubble up the requested element to the root.. In this particular question, since we have to do a rangeLookup(k1, k2), the first time we search for k1, it will bubble up to be the root.. because of this, elements close to k1 will also bubble up close to the root. Hence, when we search for elements closer to k1, they will more likely be found closer to the root, and improving performance slightly better.

Also, since you are building a data structure, it will have to support the operations of lookup, and rangeLookup not just once, but multiple times.. i.e.

I can come up with a sequence like lookup(1), lookup(5, 10), lookup(7), lookup(2,5).. your data structure has to be efficient over the course of all the above lookups, and not just one. Splay trees provide a slight advantage in such cases over a normal BST.

- Sachin December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

We don't have to use a special BST for this case.

Range lookup in normal BST is the order of O(key2-key1). You first search for key1 and then do the successor calls until you reach key key2

- prashant2361 March 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Use Red-Black/AVL Tree.
For Lookup T(N) = O(Log(N)).
For Range Lookup T(N) = O(Log(N) + Number of keys in between Key1 & Key2).

- SRB December 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you please explain what is a range lookup?

- DashDash December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you explain what is the functionality of lookup and rangelookup

- kamal December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

lookup does takes key and gives back value
rangeLookup takes two keys key1 and key2 and returns all the key value pair which lies between key1 and key2 in lexicographically order

- darklight December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This could be implemented using a hash table and a RB tree. In C++ STL, it would be a hash_map and map
1. lookup, just lookup the hash_map
2. range lookup: find the key1 in map, iterate the map to key2, and while iterating, output all the values from hash_map. O(n), where n is the # of keys between key 1 and 2.

- Walainlord December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What's the point of outputting the values from hash_map if they're already in the map that you're iterating through anyway? Perhaps you meant the (tree) map to be a (tree) set?

- eugene.yarovoi December 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not just use BST + hash?

- Anonymous December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

lookup a key can be done in O(1) with hash table
for lookup(k1, k2) range, what is the expected complexity?

- Anonymous December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since I don't remember B+ or Splay trees I would just write the following code to implement the range method. Basically I store the (key, value) in a Node and then store the Nodes in a BST. For lookup, I can either just do a binary search or have a HashMap as well as many have suggested.

static ArrayList<Node> range(Node n, int smaller, int larger) {
  ArrayList<Node> result = new ArrayList<Node>();
  boolean gteSmaller = (n.value >= smaller);
  boolean lteLarger = (n.value <= larger);
  
  if(gteSmaller && lteLarger)
    result.add(n);
  if(n.left != null && gteSmaller)
    result.addAll(range(n.left, smaller, larger));
  if(n.right != null && gteSmaller)
    result.addAll(range(n.right, smaller, larger));
  return result;
}

- Sunny January 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public ArrayList<BTNode> findNodesWithinRange(BTNode root, int min, int max){
		ArrayList<BTNode> result = new ArrayList<BTNode>();
		if(root!=null){
			if(root.getData()<=max && root.getData()>=min){
				result.add(root);
			}
			if(root.getData()>=min && root.getLeft()!=null)
				result.addAll(findNodesWithinRange(root.getLeft(), min, max));
			if(root.getData()<=max && root.getRight()!=null)
				result.addAll(findNodesWithinRange(root.getRight(), min, max));
		}
		return result;
	}

- boba January 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If the key's are ints or chars then a Hashmap would definitely be the simple enough. Though running time I guess is O(n^2) .. i.e check n items for m items within the range...

Using a BST would probably reduce each lookup time to log(n) from n.
A BST would also allow keys which are not integars .. like real numbers .. things which can't be enumerated...

just my thoughts

- srimalj January 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess a hashmap could also handle non integer values (real numbers like fractions, floats doubles) if we just iterate through all the elements and filter items inside the range.. this would be the dumb n^2 approach I guess. For a hash map with n integars looking for a range of m items would be nm ..

- srimalj January 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are also specialized trees which do range lookups:

Range_tree

K-d_tree

- srimalj January 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

given everyone is talking about O(logN), why not just a array sorted by key.......

- Anonymous February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If range is densely populated, then hash table will be the best option. You can check from a starting element to last element, whether it exists in hash table. But it begins to underperform when you have lot less elements than your range or you need huge has table to avoid collision. Otherwise you can use BST. And red black tree is the best choice here.

- Riyad Parvez July 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bst

void rangeSearch(TreeNode* root, int k1, int k2)
{
if (root != NULL)
{
if (root->val < k1) { rangeSearch(root->right, k1, k2); }
else if (root->val > k2) { rangeSearch(root->left, k1, k2); }
else { rangeSearch(root->left, k1, k2); cout << root->val << endl;
rangeSearch(root->right, k1, k2); }

}
}

- Anonymous August 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not use a hashmap, which maps a key to a value pointer and all values are stored as a linked list. Its O(1) for a lookup. For ranged queries, it would be the number of element values between key1 and key2. Please suggest

- Rahul September 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Skip list would be best solution. No needs to have two separate data structures. O(log N) for lookup and O(logN + K) for range query when K is the size of the range.

- Anonymous May 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think the best data structure is TreeMap.
Since in TreeMap the keys are sorted so range query can be easily answered i.e. do a inorder traversal.
And lookup can be done in O(1) time too.

- sanjeev December 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

tree-map is a good solution but doesnt provide O(1) look-up time efficiency, it takes O(logn) time

- spiderman December 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

wouldn't this be a typical scenario to use interval trees?

- lob4tt January 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

tree

- glk December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I mean BST

- glk December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

DynamoDB :)

- Sathish January 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

cool :)

- chiddu January 03, 2013 | 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