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 generate all valid combinations of n pairs of parentheses?
Build the string character by character. You may add an opening bracket while fewer than n have been used, and a closing bracket only when it would not exceed the number of openings. A string of length 2n is valid.
IntermediateQuick answerCoding1 min read - How do you group anagrams together from a list of words?
Anagrams share the same sorted letters, so use the sorted word as a hash map key and collect each word under its key. The map values are the groups.
IntermediateQuick answerCoding1 min read - How do you implement a queue using two stacks?
Push new items onto an input stack. When you need to dequeue and the output stack is empty, pour everything from the input stack into the output stack, which reverses the order, then pop from the output stack.
IntermediateQuick answerCoding1 min read - How do you implement quick sort, and when does it become slow?
Pick a pivot, partition so smaller elements go left and larger go right, then recursively sort both sides. It averages O(n log n) but becomes O(n squared) when the pivot is always the smallest or largest element.
IntermediateQuick answerCoding1 min read - How do you remove the nth node from the end of a linked list in one pass?
Move a fast pointer n steps ahead of a slow pointer, then move both until fast reaches the last node. The slow pointer is then just before the node to delete. A dummy head handles removing the first node.
IntermediateQuick answerCoding1 min read - How do you rotate an array to the right by k steps in place?
Reverse the whole array, then reverse the first k elements, then reverse the rest. Take k modulo the length first. It runs in O(n) time with O(1) extra space.
IntermediateQuick answerCoding1 min read - How do you solve Combination Sum, finding all combinations that add up to a target when numbers can be reused?
Sort the candidates and backtrack with a start index. Add a candidate, recurse with the reduced target (same start index so it can be reused), and stop a branch when the target is zero or the candidate exceeds it.
IntermediateQuick answerCoding1 min read - How do you solve the Container With Most Water problem using two pointers?
Start with pointers at both ends. The area is the shorter height times the distance. Move the pointer at the shorter line inward, because moving the taller one can never increase the area.
IntermediateQuick answerCoding1 min read - How do you sort an array of 0s, 1s and 2s in a single pass?
Use the Dutch national flag method with three pointers: low for the next 0 position, high for the next 2 position, and mid scanning. Swap 0s to the front and 2s to the back; leave 1s in the middle.
IntermediateQuick answerCoding1 min read - What is a stable sort and why does it matter?
A stable sort keeps items with equal keys in their original relative order. It matters when you sort by one field after another, such as by marks first and then keep the earlier name order for ties.
IntermediateQuick answerCoding1 min read - What is counting sort and when can it beat O(n log n)?
Counting sort counts how many times each value appears in a small integer range, then rebuilds the array from those counts. It runs in O(n + k) time for values in 0..k, so it beats comparison sorts when k is small.
IntermediateQuick answerCoding1 min read - What is Kadane's algorithm for the maximum subarray sum?
At each element decide: extend the running subarray or start fresh from this element. Track the best sum seen. It finds the largest contiguous sum in one pass.
IntermediateQuick answerCoding1 min read - What is the difference between merge sort and quick sort?
Merge sort splits the array in half, sorts each half and merges them; it is always O(n log n) and stable but needs O(n) extra space. Quick sort partitions around a pivot in place; it averages O(n log n) but can degrade to O(n squared) with bad pivots.
IntermediateQuick answer1 min read - How do you design an LRU cache with O(1) get and put?
Combine a hash map for instant lookup with an ordering of recent use. In JavaScript a Map remembers insertion order, so deleting and re-inserting a key on each access moves it to the newest end; the oldest key is evicted when full.
AdvancedQuick answerCoding1 min read - How do you find the maximum in every sliding window of size k?
Keep a deque of indices whose values are in decreasing order. Drop indices that left the window from the front, drop smaller values from the back before adding the new index, and the front is always the window maximum.
AdvancedQuick answerCoding1 min read - How do you find the node where a cycle begins in a linked list?
Detect the cycle with slow and fast pointers. When they meet, reset one pointer to the head and move both one step at a time. They meet again exactly at the start of the cycle.
AdvancedQuick answerCoding1 min read - How do you solve the N-Queens problem with backtracking?
Place one queen per row. For each column in the row, check that no earlier queen shares the column or a diagonal. If safe, place it and recurse to the next row; otherwise skip. Undo and try the next column.
AdvancedQuick answerCoding1 min read