Re:Ask HN: What are the best interview questions you've been...
by antoaravinth on Tuesday February 21, 2017 @08:00PM
(#13701918)
Find the minimum value in the stack in O(1) both time and space complexity.
[ Parent
| on HN ]
Re:Ask HN: What are the best interview questions you've been...
by api_or_ipa on Wednesday February 22, 2017 @02:47AM
(#13703919)
I have a novel solution: use a delta ~~queue~~ stack! Instead of pushing straight numbers, push the delta of each new value against the next smaller value currently in the stack. Sadly I can't find a resource for this data structure. I learned about it in university when writing a sleep() function for an OS I was writing. On each tick, you simply decrement the first value (or pop it if it's 0).
[]
push(1) : [1]
push(2) : [1,1]
push(3) : [1,1,1]
push(3) : [1,1,1,0]
pop() : 3, [1,1,1]
pop() : 3, [1,1]
pop() : 2, [1]
pop() : 1 [0]
Re:Ask HN: What are the best interview questions you've been...
by daysforbeef on Wednesday February 22, 2017 @01:53AM
(#13703659)
Have a few questions .. (1) doesn't it seem like this only works for elements that have an ordering as well a binary operation multiplication, subtraction and addition defined on them, making it too specific ? (I'm talking about the whole 2x - minelement) (2) If I have a variable hanging around a stack, is it still a stack? (3) When I pop out the min value in the stack, and want the next min Value, how do I get that,? seems like the previous min value still sticks around? (4) With comparisons and 1 value that hols state, do I really need to transform the values going into the stack?
Re:Ask HN: What are the best interview questions you've been...
by glandium on Tuesday February 21, 2017 @08:40PM
(#13702132)
Do you mean "design a stack where you can find the minimum value in O(1) both time and space complexity"? Because I don't see how you can do O(1) with an arbitrary stack.
Re:Ask HN: What are the best interview questions you've been...
by antoaravinth on Tuesday February 21, 2017 @10:44PM
(#13702825)
Yes thats right, you can come up with any solution. But make sure O(1) is satisfied (both in time and space complexity).
Anyways I had failed to give proper answer and I was rejected in the interview. This was the last question of my last round!
Spoiler: Here is one such solution using a simple but very clever trick: http://www.geeksforgeeks.org/design-a-stack-that-supports-ge...
Re:Ask HN: What are the best interview questions you've been...
by balamaci on Wednesday February 22, 2017 @05:51AM
(#13704584)
What field of programming have you implemented that? I'm curios, is it in kernel development, some DB design. Not trolling, just curios where that usecase where this would come in handy and why ask it if it's not a direct relation to what you are doing.
Re:Ask HN: What are the best interview questions you've been...
by wadeboggs on Tuesday February 21, 2017 @08:56PM
(#13702221)
Make another stack with the latest minimum, then when you pop that top value, you also pop it off the minimums stack. Then you have the last minimum before that one.
Is that it?
Re:Ask HN: What are the best interview questions you've been...
by sand500 on Tuesday February 21, 2017 @10:05PM
(#13702638)
What if you added values that are greater then the minimum at the top of the minimum stack but less than the next value on the minimum stack?
Re:Ask HN: What are the best interview questions you've been...
by antoaravinth on Tuesday February 21, 2017 @10:46PM
(#13702832)
Your using two stacks now. This is the solution that I had told but your not satisfying the condition w.r.t to space , its O(n) not O(1).