Interview Q&A / Data Structures & Algorithms
Data Structures & Algorithms interview questions
Arrays, linked lists, hashing, trees and patterns, with line-by-line code and complexity.
Prefer rapid revision? Practise Data Structures & Algorithms short interview answers →
- How do you check if a string of brackets is valid (balanced parentheses)?
Scan the string with a stack. Push every opening bracket. For every closing bracket, the stack must be non-empty and its top must be the matching opening bracket, which you then pop. The string is valid only if the stack is empty at the end. O(n) time, O(n) space.
BeginnerIn-depthCoding3 min read - How do you evaluate a postfix (reverse Polish) expression?
Scan tokens left to right. Push numbers onto a stack. When you see an operator, pop two numbers, apply the operator (the first popped is the right operand), and push the result. The final stack value is the answer.
BeginnerQuick answerCoding1 min read - What is the difference between a stack and a queue, with real examples?
A stack is last in, first out (LIFO): you add and remove at the same end. A queue is first in, first out (FIFO): you add at the back and remove from the front.
BeginnerQuick answer1 min read