Tuesday, February 28, 2012

Stack - push, pop, max operations in constant time

One good question I was asked in one of the interview in my career.
Implement Integer value Stack such a way that all the following operations are constant operation, i.e. O(1).
void push(int n) -> Pushes element n on the stack
int pop() -> Pops element from the top of the stack and return it.
int max() -> Returns maximum of all elements contained in the Stack at that moment.

Isn't it a good Question?

7 comments:


  1. Hayatın karmaşası içinde bazen kısa ve öz hikayelere ihtiyaç duyarız. Bu nedenle, zaman zaman kısa romanlar okuyarak farklı dünyalara yolculuk yapmayı seviyorum. Bu eserler, anlatmak istediği duyguları kısa sürede etkili bir şekilde yansıtır. İnsanlar, yoğun temposunda bile birkaç sayfa ile kendilerini yeniden keşfedebilirler. Kısacası, kısa romanlar hayatın küçük ama anlamlı anlarını yakalamak için ideal bir yoldur.

    ReplyDelete