DS & Algorithms
Data Structures & Algorithms Interview Questions
Data structures and algorithms (DS&A) form the backbone of every technical interview at top tech companies. Whether you're interviewing at a FAANG company or a fast-growing startup, expect to spend the majority of your coding round solving problems that test your ability to choose the right data structure and apply an efficient algorithm.
The good news: DS&A interviews test a surprisingly finite set of patterns. Once you recognise that a problem is really just a BFS traversal, a sliding-window scan, or a classic DP recurrence, the solution becomes almost mechanical. This guide covers the core concepts you need to internalise before your next interview.
Key Concepts
Arrays & Hashing
The two-pointer and sliding-window techniques solve the majority of array problems in O(n) instead of O(n²). Hash maps trade space for speed — use them whenever you need O(1) lookups for counting, grouping, or checking membership. Classic patterns: two-sum, longest substring without repeating characters, group anagrams.
Linked Lists
Learn the fast/slow pointer (Floyd's cycle detection), reversing a list in-place, and merging two sorted lists. Interviewers love these because they're easy to state but expose whether you think carefully about pointer manipulation and edge cases like single-node or empty lists.
Trees & Binary Search Trees
Most tree problems reduce to DFS (pre/in/post-order) or BFS (level-order). Know the recursive structure cold. Binary Search Trees add the invariant that lets you prune whole subtrees: left < root < right. Common problems: lowest common ancestor, validate BST, serialize/deserialize.
Graphs
Represent graphs as adjacency lists. BFS finds shortest paths in unweighted graphs; DFS is used for cycle detection, topological sort, and connected components. Union-Find (disjoint set) is worth knowing for problems asking whether two nodes are connected. Practice: number of islands, course schedule, clone graph.
Sorting & Searching
Know merge sort (O(n log n), stable) and quicksort (O(n log n) average, in-place) at the conceptual level. Binary search extends beyond sorted arrays — apply it any time the answer space is monotonic. Heap/priority queue gives you O(log n) access to the min or max element, which is essential for top-k problems.
Dynamic Programming
DP solves problems with overlapping subproblems and optimal substructure. Start by defining the state and recurrence clearly before writing code. Most DP problems belong to one of a handful of templates: 0-1 knapsack, unbounded knapsack, longest common subsequence, edit distance, or coin change. Recognising which template applies is the hard part.
Sample Interview Questions
What is the time complexity of searching in a balanced BST, and why?
Answer: O(log n)
Why it matters: A balanced BST has height O(log n). Each comparison eliminates half the remaining nodes, so at most O(log n) comparisons are needed to find any element.
Which data structure would you use to find the k-th largest element in a stream of integers?
Answer: A min-heap of size k
Why it matters: Maintain a min-heap of the k largest elements seen so far. The heap root is always the k-th largest. Each insertion is O(log k), and the answer is available in O(1).
What is the difference between BFS and DFS, and when would you prefer each?
Answer: BFS explores level by level (uses a queue); DFS goes deep before backtracking (uses a stack or recursion).
Why it matters: Use BFS when you need the shortest path in an unweighted graph. Use DFS when you need to explore all paths, detect cycles, or perform topological sorting.
Ready to test yourself?
Apply what you've read with a timed 10-question quiz on DS & Algorithms.
Start DS & Algorithms Quiz →