Google Interview Question for Software Engineer / Developers






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

Use a Hashmap along with doubly linked list.
Insertion: When an element is inserted into the hashmap create a new node at the front of the doubly linked list. The hashmap entry will have the reference to this node in the douly linked list. Also the node in the liked list will have a reference to the entry in the hashmap.
So Insertion : O(1)
Deletion: Delete the entry from the hashmap and also following the reference to the doubly linked list, delete the node too.
So O(1)
Access: Get the element in the hashmap, follow the reference to the doubly linked list and just move this node in the doubly linked list to the front of the list.
Recently used: Get the first element from the linked list.
So Access: O(1)

- Thiyaneshwaran S November 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why Double Linked List is required? Can we do that by using Single Linked List?

- Creation November 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

perfect solution

- Anonymous May 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You need a doubly link list to be able to delete and move the item to the beginning. With a singly linked list, you would need to also have (apart from the pointer to the node to be removed) the pointer to the 'prev' element to delete/move an item.

- Anonymous March 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We do not need a doubly linked list. The list is always sorted in the LRU ordering.

if(cache miss)
        head is the candidate for removal 
   else if(cache hit){
        // we have to put the cache hit object to the end of the list
         if(cache hit object is tail)
               do nothing
       else
         copy the next node to current node and remove the next node
    }

- govind July 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using a Min Heap with Least recently used node(based on time stamp of access) as the root?

- Anonymous November 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

even insertion and deletion should be O(1)

- Anonymous November 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think deletion cannot be O(1), even though you use Fibonacci heap.

- Anonymous November 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if you use a hash table, and separately maintaining the least recently used item in another variable / file / hardware unit. Then whenever a new item is inserted, you could check if its last usage is older than the current max, and replace if necessary. The problem with this is that when the max item gets deleted, finding the next max item is no longer O(1).

One possible way to solve this is by using a stack. Ie, when a node is inserted, if its last used time is greater than the top of the stack, push it on. When a node gets deleted, check if it is the top element of the stack. If so, pop, and the next operation becomes the LRU. If the deleted element is present elsewhere on the stack, don't worry about it. Eventually when you do get down it, you can check back with the hash table to see if it exists. If it doesn't, pop and move down to the next node.

However, this still has a problem. Since this is a cache we're talking about, its perfectly possible that the timestamps on any of the elements on the stack could change after they've been pushed. For instance, let the memory locations be a,b,c,... Initially, the stack is ordered as:
b
a
c
--
Now, 'a' gets accessed, so strictly speaking, the stack should look like:
b
c
a
--
but it doesn't. So if 'b' gets deleted, then we get a faulty answer from the stack. This is where I'm stuck, and I would look at the interviewer for a clue... ;)

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

Use a doubly linked list, instead of stack. Whenever an item is accessed remove it and put it at the top. The LRU item will always be at the end. (i.e tail). You should have a pointer to tail(say T) to remove LRU item fast. Its a double linked list instead of single, bcos you can update the T with T->prev

- Anonymous November 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Best Answer

- Avinash November 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how to find an item in O(1)

- Anonymous November 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

will be slow for multi-threaded code.. since u need to lock and unlock list of each insert and delete operation

- Anonymous June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Queue Implementation with Doubly Linked List..

- Anonymous November 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Again the problem is finding an element in O(1)

- Chanakya August 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

good one by Thiyaneshwaran. just to add one point : To find least recently used item when u have full emory and u want to delete any item before u can insert the next one. it will be at the tail of doubly linked list.
but here space used: 2*content size+2*(space for pointer)*no of items
space used is too much dont u think so ??

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

most data structures in linux kernel are implemented as combination of hash tables and linked lists. There are tradeoffs but we have to live with it.

- Anonymous January 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Splay tree??

- Mr Bean November 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a hash table + a sorted queue (maintained without ever sorting). Link each list entry with its hash table entry. The link ensures that the list need not be traversed to find a particular entry.This ensures that any operation runs O(1).

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

Java 7 LinkedHashMap looks like Hashmap + linked list out-of-the-box.

- Ciro Santilli December 10, 2015 | 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