Implement Stack with in-place

Last updated: December 2, 2025

Quick Overview

Implement a stack data structure that supports push, pop, and top operations using only in-place operations. The stack should allow for constant time complexity for push and pop operations, and you should ensure that the space complexity remains O(1). Your implementation should also handle edge cases, such as popping from an empty stack.

MongoDB
Coding & Algorithms
Software Engineer
MongoDB
December 2, 2025
Software Engineer
Onsite
Coding & Algorithms
Medium

23

1

3,070 solved


Implement a stack data structure that supports push, pop, and top operations using only in-place operations. The stack should allow for constant time complexity for push and pop operations, and you should ensure that the space complexity remains O(1). Your implementation should also handle edge cases, such as popping from an empty stack.

This coding problem is frequently asked during Onsite at MongoDB. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. MongoDB expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Binary search and divide and conquer
Dynamic programming and memoization
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Sorting and searching
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would your solution change if the input was sorted?
  • What happens if the input contains duplicates?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • How would you test this solution thoroughly?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To implement a stack with in-place operations, the key constraint is that we need to develop a structure that allows for constant time complexity for both push and pop operations while utilizing O...

Approach

We can achieve this by using a linked list-like approach where we manage our stack using a single variable to represent the top of the stack. Each node will contain the value and a reference to the ne...


Submit Your Answer
Markdown supported

Related Questions