Stacks and queues interview questions
Stacks for matching and undo, queues for order of arrival, monotonic stacks for 'next bigger' questions, and how to build one from the other.
– interview-ready · 12 questions in this topic
– of 12 tried
Saved on this device. Interview-ready means you got it right on two different days. Guesses and hints count for less.
Questions
- Do the brackets in a line balance?Predict
- How many days until a warmer day?Pick the fix
- A stack that also reports its minimumPredict
- Evaluate an expression written in postfix orderPredict
- Apply the backspaces in typed textPredict
- Build a queue out of two stacksPredict
- Tidy a file path with . and .. partsPredict
- Largest poster that fits under a skylinePredict
Practise this topic
Flashcards · 22
Flip, then say if you knew it. Needs JavaScript; every answer is also on its question page.
Quick quiz · 12 questions
One exercise per question, checked as you go. Needs JavaScript.
Mock interview · 2 sets
Timed, out loud, then tick what you covered. Needs JavaScript.
Quick facts
Recall the answer first, then tap Show to check.
Python list as a stack
appendandpop()at the end of a list are O(1) amortised, so a list is the usual Python stack.collections.dequedequeis the Python queue:appendto the back andpopleftfrom the front, both O(1).queue.Queuevsdequequeue.Queueadds locking for threads; for single-threaded algorithmsdequeis simpler and faster.Kotlin
ArrayDequeArrayDequeworks as a stack (addLast, removeLast) and a queue (addLast, removeFirst) with amortised O(1) operations.int()truncates toward zeroint(-7 / 2)is -3 while-7 // 2is -4; floor division rounds down,int()of a float truncates toward zero.
Printable cheat sheet · Review your weak spots in this topic
Share your progress
Sources: Topic outline informed by the Tech Interview Handbook by Yangshun Tay and the NeetCode 150 pattern grouping by neetcode-gh, both MIT licensed. Every problem, solution and explanation is written by Best Latest Apps. Credits and license