Data Structures & Algorithms Interview Questions (2026)
Data structures interview questions for 2026: 18 real DSA problems with concise answers, Big-O complexity, and patterns coding interviews actually test.
Data structures and algorithms are the gatekeeper of nearly every software engineering interview, from startups to FAANG. The good news: the questions cluster into a handful of patterns. Once you recognize “this is a two-pointer problem” or “this needs a hash map,” most rounds become tractable. Below are 18 real questions, ordered beginner to advanced, each with a concise answer, the key insight, and complexity — the things interviewers actually score.
Beginner DSA questions
1. What is Big-O notation, and why does it matter? Big-O describes how runtime or memory grows as input size grows, ignoring constants. O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n), O(n²) quadratic, O(2ⁿ) exponential. It matters because an O(n²) solution that’s fine for 100 items dies at 1,000,000. Always state the time and space complexity of your solution.
2. Array vs linked list — when do you use each? Arrays give O(1) random access by index but O(n) insertion/deletion in the middle (shifting). Linked lists give O(1) insertion/deletion if you hold the node, but O(n) access (no indexing) and worse cache locality. Use arrays by default; linked lists when you insert/remove at ends frequently.
3. How does a hash map achieve O(1) lookup? It hashes the key to a bucket index, so lookups skip scanning. Average case is O(1); worst case O(n) if everything collides into one bucket. The trade-off vs an array/list is memory and unordered storage. Hash maps are the single most useful interview tool — reach for one whenever you need fast lookups or counting.
4. What’s the difference between a stack and a queue?
A stack is LIFO (last in, first out) — push/pop, used for undo, recursion, and DFS. A queue is FIFO (first in, first out) — enqueue/dequeue, used for BFS and task scheduling. Both are O(1) for their core operations.
5. Reverse a string in place.
def reverse(s): # s is a list of chars
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
O(n) time, O(1) space — the two-pointer pattern in its simplest form.
Intermediate DSA questions
6. Find if an array has two numbers summing to a target.
def two_sum(nums, target):
seen = {}
for i, n in enumerate(nums):
if target - n in seen:
return [seen[target - n], i]
seen[n] = i
return []
The naive nested loop is O(n²); the hash map drops it to O(n) time, O(n) space. This is the canonical “use a hash map” problem.
7. Detect a cycle in a linked list.
Floyd’s tortoise-and-hare: a slow pointer moves one step, a fast pointer two. If they meet, there’s a cycle; if fast hits null, there isn’t. O(n) time, O(1) space — no extra set needed.
8. What is binary search, and what’s its complexity?
On a sorted array, repeatedly halve the search range by comparing the middle element. O(log n) time. The classic bug is the loop boundary — use while left <= right and mid = left + (right - left) // 2 to avoid overflow and off-by-one errors.
9. Explain BFS vs DFS. BFS explores level by level using a queue — best for shortest path in an unweighted graph. DFS goes deep using recursion or a stack — best for path existence, cycle detection, and topological sort. Both are O(V + E). Choose BFS when you need the nearest answer, DFS when you need to exhaust a branch.
10. How do you find the kth largest element efficiently? A min-heap of size k: push elements, pop when size exceeds k; the heap’s root is your answer. O(n log k) time, O(k) space — better than sorting the whole array (O(n log n)) when k is small. Quickselect averages O(n) but has O(n²) worst case.
11. What’s the sliding window pattern? Give a use case. Maintain a window (two pointers) over a contiguous range, expanding and shrinking it instead of recomputing from scratch. Example: longest substring without repeating characters — grow the right edge, and when a duplicate appears, move the left edge past it. Turns an O(n²) scan into O(n).
Advanced DSA questions
12. Explain dynamic programming. When do you use it? DP solves problems with overlapping subproblems and optimal substructure by caching subresults. Two styles: top-down (recursion + memoization) and bottom-up (build a table). Classic signs: “count the ways,” “min/max cost,” “longest/shortest.” Example — Fibonacci goes from O(2ⁿ) recursion to O(n) with memoization.
13. Solve the coin change problem (minimum coins).
def coin_change(coins, amount):
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for c in coins:
for x in range(c, amount + 1):
dp[x] = min(dp[x], dp[x - c] + 1)
return dp[amount] if dp[amount] != float('inf') else -1
O(amount × coins) time. The insight: dp[x] = best way to make x, built from smaller amounts.
14. What is a trie, and where is it used? A tree where each node represents a character, so words sharing prefixes share paths. Lookup/insert is O(L) where L is word length — independent of how many words are stored. Used for autocomplete, spell-check, and prefix search. The cost is memory.
15. How would you detect a cycle in a directed graph? DFS with three states per node: unvisited, in-progress (on the current path), done. If DFS reaches an in-progress node, there’s a back edge — a cycle. This is also the basis of topological sort (which only exists if the graph is acyclic).
16. Explain quicksort vs mergesort. Both average O(n log n). Quicksort sorts in place (O(log n) stack space) but degrades to O(n²) on bad pivots — randomize the pivot. Mergesort guarantees O(n log n) and is stable, but needs O(n) extra space. Mergesort suits linked lists and external sorting; quicksort is usually faster in practice on arrays.
17. How do you find the lowest common ancestor in a binary tree?
Recurse: if the current node is null or equals either target, return it. Recurse left and right; if both sides return non-null, the current node is the LCA; otherwise return whichever side is non-null. O(n) time.
18. What’s the difference between memoization and tabulation? Both are dynamic programming. Memoization is top-down — recurse and cache results as you go, computing only the subproblems you need. Tabulation is bottom-up — fill a table iteratively from base cases, avoiding recursion overhead and stack limits. Same complexity; tabulation is often faster and safer for large inputs.
How to stand out in a coding interview
The pattern-spotting matters more than memorizing solutions. Out loud, name the pattern (“this is a two-pointer / sliding-window / DP problem”), state the brute force and its Big-O, then optimize and state the new Big-O. Walk through one example by hand, mention edge cases (empty input, single element, duplicates, negatives), and only then code. Interviewers score the process, not just a passing solution.
Rehearse explaining your approach out loud against a clock — talking while coding is the skill most people under-practice. Run full DSA rounds with OnJob’s AI mock interviews and get a confidence score on how clearly you communicate, then create a free account to find matched software engineering roles. Pair this with our Google interview questions and system design interview guide for a full SDE prep loop.
FAQ
What DSA topics are most asked in interviews in 2026? Arrays and strings (two-pointer, sliding window), hash maps, linked lists, stacks and queues, trees and graphs (BFS/DFS), heaps, binary search, and dynamic programming. Big-O analysis is expected on every answer. FAANG loops lean harder on graphs and DP.
Do I need to memorize every algorithm? No — recognize patterns instead. Most problems reduce to a handful: two-pointer, sliding window, hashing, BFS/DFS, binary search, heaps, and DP. Practice ~150 well-chosen problems across these patterns rather than grinding hundreds blindly, and you’ll handle unseen variations.
How should I communicate during a coding round? Restate the problem and clarify constraints, state the brute-force approach and its complexity, propose an optimization with its complexity, walk through a small example, then code while narrating. Finish by testing edge cases. This running commentary is exactly what interviewers grade alongside the code.
Ready to put this into action?
Create your free OnJob profile and let AI match you to jobs you can actually win.
Create my free profileFree OnJob tools & guides
Related reading
Google Interview Questions & How to Prepare (2026)
The real Google interview process in 2026 — every round, sample coding, behavioural and system-design questions with answers, plus an 8-week prep plan.
InterviewsJava Interview Questions with Answers (2026)
Java interview questions for 2026: 18 real questions with concise answers and code on OOP, collections, the JVM, concurrency, and streams for SDE prep.
InterviewsPune Government Jobs 2025
Find Pune Government Jobs 2025 with latest Pune recruitment, walk in interview in Pune, jobs in Pimpri Chinchwad, Maharashtra govt jobs, Mahabharti & MahaSarkar updates.
InterviewsPython Interview Questions with Answers (2026)
Python interview questions for 2026: 18 real problems with concise answers and code on data types, decorators, the GIL, and generators interviewers test.