Google Interview Question for Software Engineer in Tests






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

if you just remove the word cache from the Qs it seems to be pretty simple...just use a MaxHeap to keep track of the max in a decreasing order and to retrieve the max at any time...

- googler August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For normal cache operation, we can use LRU (least recently used) algorithm. It has various implementations like stack, time counter. For maintaining max no. data, we can check the current data against the last max data.

- bebo August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"check the current data against the last max data." is not enough to keep track of the max. Suppose you are inserting a lot of entries with small valuesi into the cache, and it is finally causing the cache to be full and the current max happens to be the one to be evicted.
Now you don't know the next max value unless you go through the whole cache again.

- pwz September 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how do ensure "efficient retrieval of the maximum element"?

using LRU and a different technique to pick the max are contradictory to each other. i guess you got the point! so your approach will not work. Else prove otherwise.

- googler August 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Like googler, the first thing that came to my mind was a Heap.
However, bebo's approach might have some weight. If you lay down the data as a non-tree data structure and find the max element upfront and then with each incoming element you check if its greater than the current max, you'd always have a pointer to the max element. ie, max element retrieval is O(1) just like maxheap. The rest of the cache ops can be done by LRU or other algos as required.
Having said that I'd give the maxheap soln anyday.

- hgk August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a

- a September 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't this a priority queue? it could be implemented as a heap, but it can also implemented as the other data structure.

- le September 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this. We maintain a heap to model a cache and with that said we have access to the max element which shall be at its root.
In an event of retrieval only {cache hit}, we get the maximum element in O(1).
In case of an insert {cache miss} into the cache without a page swap, we can compare the root with the inserted element and decide on the max.
In case of an insert {cache miss} accompanied by a page swap out, we may have to reheap the cache contents after the swappping & insert happens, as we are not sure
if the previous maximum has been swapped out or not and/or if its greater than the
newly inserted element.

- amit September 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Any thoughts about the same ?

- amit September 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Heap + LRU = Priority Queue (where recent items in the queue have highest priority)

- Anonymous October 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would use a splay tree here. They've got the great propery of always keeping the last accessed node as the root and recently used items close to the root, so it's a great cache...and they are a normal, well balanced BST so we can perform all sorts of operations on it quickly (find min, max, etc...)

- trey November 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how do you retrieve an item? heap stores the max and min item at the root. But not good at finding an element.

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

how about min-max heap I mean double ended priority queue? min heap will store the age value and max heap will store the maximum magnitude of the element. And as in min-max implementation the same elements in both heaps can point to each other.

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

linkedhashmap

- Anonymous June 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect
LinkedHashMap is the answer

- erappy June 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use max-heap + hashtable + linked-list (latter two is for LRU). Read question as implement a LRU cache supporting the retriveal of max-element like cache.getMax()

- Anonymous July 30, 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