DSA Interview Questions: Commonly Asked Questions with Answers
DSA Interview Questions: Commonly Asked Questions with Answers
46 min read
Walking into a DSA interview without knowing what to expect is the fastest way to freeze up on a question you could otherwise solve. This guide from Unrated Coder collects the DSA interview questions that come up most often — organized by topic, explained clearly, and paired with the reasoning interviewers actually want to hear, not just the final answer.
DSA interview questions typically cover arrays, strings, linked lists, stacks, queues, trees, graphs, and dynamic programming, testing both your ability to solve a problem and explain your time and space complexity. Interviewers care as much about how you approach a problem out loud as they do about the final working code.
What Interviewers Are Actually Testing
Before getting into specific questions, it helps to understand what's really being evaluated. A DSA interview isn't just about producing correct code — it's about your problem-solving process, your ability to reason about efficiency, and how clearly you can explain your thinking. Two candidates can arrive at the same correct answer and be rated very differently based on how they got there.
Array and String Questions
1. How do you find the missing number in an array of 1 to n?
The efficient approach uses the sum formula: calculate the expected sum of numbers from 1 to n using n*(n+1)/2, then subtract the actual sum of the array. The difference is the missing number. This runs in O(n) time and O(1) space, which is what interviewers look for over a brute-force nested loop.
2. How do you reverse a string without using a built-in reverse function?
Use two pointers — one at the start, one at the end — and swap characters while moving both pointers toward the center until they meet. This solves it in O(n) time with O(1) extra space, which is the detail that separates a strong answer from an average one.
3. How do you find the first non-repeating character in a string?
Build a frequency map of each character using a hash map in one pass, then iterate through the string a second time and return the first character with a count of 1. This runs in O(n) time overall, and explaining the two-pass logic clearly is usually more important than the code itself.
4. What's the difference between a subarray and a subsequence?
A subarray is a contiguous block of elements from the original array, while a subsequence maintains relative order but doesn't need to be contiguous. This distinction comes up constantly in follow-up questions, so it's worth being able to state clearly and quickly.
Linked List Questions
Continue Learning
Explore more insights and tutorials to enhance your skills
The standard approach is Floyd's Cycle Detection (the "slow and fast pointer" technique) — move one pointer one step at a time and another two steps at a time. If they ever meet, a cycle exists. This runs in O(n) time and O(1) space, and it's one of the most frequently asked linked list questions across all experience levels.
6. How do you reverse a linked list?
Iterate through the list while keeping track of the previous, current, and next nodes, reversing each next pointer as you go. Being able to draw this out on a whiteboard, node by node, usually matters more in the interview than writing the code from memory.
7. How do you find the middle of a linked list in one pass?
Use the slow and fast pointer technique again — the slow pointer moves one step at a time while the fast pointer moves two steps. When the fast pointer reaches the end, the slow pointer is at the middle.
Stack and Queue Questions
8. How do you check if parentheses in a string are balanced?
Push opening brackets onto a stack, and when a closing bracket appears, check whether it matches the top of the stack. If everything matches by the end and the stack is empty, the parentheses are balanced. This is a classic stack problem that interviewers use to check whether you reach for the right data structure instinctively.
9. How do you implement a queue using two stacks?
Use one stack for enqueue operations. For dequeue, if a second stack is empty, pop everything from the first stack into it (reversing the order), then pop from the second stack. This gives amortized O(1) time per operation and is a common follow-up once basic stack/queue questions are answered correctly.
Tree and Graph Questions
10. What's the difference between BFS and DFS, and when would you use each
BFS explores level by level using a queue and is ideal for finding the shortest path in an unweighted graph. DFS explores as deep as possible before backtracking, using a stack (or recursion), and is better suited for problems like detecting cycles or exploring all possible paths.
11. How do you check if a binary tree is balanced?
For each node, calculate the height of the left and right subtrees recursively, and check whether the difference is more than 1 at any point. If any subtree is unbalanced, the whole tree is unbalanced. Explaining why this requires calculating height bottom-up (not top-down) often reveals how well you understand recursion.
12. How do you find the lowest common ancestor (LCA) in a binary search tree
Starting at the root, compare both target values to the current node. If both are smaller, move left; if both are larger, move right; the moment they diverge, that node is the LCA. In a BST, this takes advantage of the sorted structure to avoid checking every node, unlike a general binary tree.
Dynamic Programming Questions
13. How do you solve the Fibonacci sequence efficiently?
The naive recursive solution runs in exponential time because it recalculates the same subproblems repeatedly. Using memoization (storing already-computed results) or converting it to a bottom-up iterative solution brings it down to O(n) time — this is usually the first question interviewers use to introduce DP concepts.
14. What's the difference between memoization and tabulation?
Memoization is a top-down approach that stores results as they're computed during recursion, while tabulation is a bottom-up approach that builds a solution iteratively from the smallest subproblem upward. Both avoid redundant work, but tabulation typically avoids recursion overhead and stack depth issues.
15. How do you solve the 0/1 Knapsack problem?
Build a table where each cell represents the maximum value achievable with a given weight capacity and a subset of items, filling it row by row based on whether including or excluding the current item gives a better result. Walking through a small example (3-4 items) on paper is usually the clearest way to explain this in an interview.
How to Actually Prepare for These Questions
Memorizing answers to a fixed list of questions doesn't transfer well when the interviewer changes even one detail. A more durable approach:
Understand the pattern behind each question (two pointers, sliding window, BFS/DFS, DP), not just the specific solution
Practice explaining your approach out loud before writing any code
Always state the time and space complexity, even if the interviewer doesn't ask
Solve variations of the same problem — once you can solve "find the missing number," try "find the two missing numbers" to test real understanding
Frequently Asked Questions
What are the most commonly asked DSA interview questions?
The most common questions cover array and string manipulation, linked list operations like cycle detection and reversal, stack-based problems like balanced parentheses, and tree traversals. Companies also frequently test dynamic programming basics like Fibonacci and the Knapsack problem.
How many DSA questions should I practice before an interview?
Solving 150-200 well-chosen problems across all major topics is generally enough to recognize common patterns and handle most interview questions confidently. Quality and pattern recognition matter more than the total number solved.
Do interviewers expect the optimal solution immediately?
Not always — most interviewers value a working brute-force solution followed by a clear explanation of how to optimize it. Jumping straight to a memorized optimal answer without being able to explain the reasoning is often viewed less favorably than a logical progression.
Should I explain my thought process while solving DSA problems in an interview?
Yes, thinking out loud is one of the most important parts of a DSA interview, since it shows the interviewer how you approach problems, not just whether you reach the right answer. Silence during problem-solving often makes interviewers assume you're stuck, even when you're not.
How important is time and space complexity in interview answers?
It's very important — most interviewers specifically ask for Big O analysis after a working solution, and being unable to state it clearly is a common reason strong coders lose points. Stating complexity without being asked also signals stronger fundamentals.
Conclusion
DSA interview questions repeat the same underlying patterns far more than they repeat exact problems, which is why understanding the reasoning behind each answer matters more than memorization. Practice these question types, get comfortable explaining your thinking out loud, and always follow up with time and space complexity. For more interview prep guides like this one, keep following Unrated Coder.