Amazon Interview Question for Software Engineer / Developers






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

the question appears to be simple. we can use a stack for this. push and pop are o(1) as done at the same end of list and min (i assume it to be the least recently used) is also o(1). Am i missing something here?

- peeyush.fun July 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use stack & mantain minimum heap order property for finding min in o(1) time.
correct me if wrong.

- vijay October 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how can we use stack for LRU it must be queue as we have to discard/pop the oldest item.

what is min in LRU?
is it data value or time stamp only

- Arpit March 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the solution is to have a list sorted by recently used order. So, there will be MRU on one end and LRU on other.
Push: add to MRU position, head of queue: const time
Pop: delete from LRU position, tail of queue: const time
find min: return LRU: const time.

Only thing which takes O(n) time is access. You have to find the entry and bring it to MRU position.

- G April 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can maintain 2 Linked Lists.
1. Stack List: This is simple & implements Push & Pop. Whish is or order(1).
2. Another List called "Min List" need to be maintained. Every Min number will be added to list.

For example, if you have below list.
2,5,4,6,1,8,0

Parse1:
Stack List: 2
Min List: 2
Here Order is O(1) for both.
Parse2:
Stack List: 5,2
Min List: 2 (Don’t insert because 5 is not min compare to 2)
Here Order is O(1) for both.
Parse3:
Stack List: 4,5,2
Min List: 2 (Don’t insert because 4 is not min compare to 2)
Here Order is O(1) for both.
Parse4:
Stack List: 6,4,5,2
Min List: 2 (Don’t insert because 6 is not min compare to 2)
Here Order is O(1) for both.
Parse5:
Stack List: 1,6,4,5,2
Min List: 1,2 (Insert it because 1 less min compare to previous min 2)
Here Order is O(1) for both.
Parse6:
Stack List: 8,1,6,4,5,2
Min List: 1,2 (Don’t insert because 8 is not min compare to 1)
Here Order is O(1) for both.
Parse7:
Stack List: 0,8,1,6,4,5,2
Min List: 0,1,2 (Insert it because 0 less min compare to previous min 1)
Here Order is O(1) for both.

At any point you will get the Min & Push with O(1).
Min: 0 (The first element. This is always like this.)

Regarding Pop.

Pop:
Value Output: 0
Change Min List to remove 0: 1,2 (1 is the Min of the list).
It’s Order(1).

Pop:
Value Output: 8
Min List is: 1,2

At any given point Push, Pop & Min are O(1)

- Naresh Nune August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your pop operation will require a linear scan to find the tail. You will have to use a DLL if you want to cache the tail.

- abhi_284 January 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the simplest solution is the LinkedHashMap implementation.

- hinax January 26, 2014 | 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