Algorithm on interview
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Algorithm questions in interviews are not just about producing a correct answer. Interviewers usually care about how you clarify the problem, choose a data structure, reason about complexity, and respond when the first approach is too slow.
What Interviewers Usually Evaluate
A typical algorithm interview measures several things at once:
- whether you can turn an ambiguous prompt into a precise problem
- whether you can identify the brute-force baseline
- whether you can improve that baseline with a better data structure or pattern
- whether you can explain time and space complexity clearly
That is why strong candidates talk while solving. Silence makes it look like guessing, even if the final code is correct.
Start with the Problem Shape
Most interview questions fall into a small set of families:
- arrays and strings
- hash-map lookups
- two pointers or sliding window
- recursion and backtracking
- tree or graph traversal
- dynamic programming
You do not need a memorized trick for every problem. You do need to recognize the shape quickly. For example, if the problem asks for the shortest number of moves in an unweighted graph, that is usually breadth-first search, not depth-first search.
Show the Baseline Before the Optimization
Interviewers often want to see whether you can reason from a naive solution to a better one. Suppose the question is "find two numbers that add to a target." A brute-force solution checks every pair:
That solution is easy to understand, but it is O(n^2). The improvement is to trade space for speed with a hash map:
Now the expected time is O(n) with O(n) extra space. Even if you know the optimized version immediately, it is often worth stating the brute-force idea first and explaining why you are not using it.
Talk About Edge Cases Early
Interview mistakes often come from skipping input assumptions. Before coding, ask or state things like:
- Can the input be empty?
- Are duplicates allowed?
- Do I need one answer or all answers?
- Is the data sorted?
- What should happen if no solution exists?
That short clarification step shows discipline and prevents rework.
Choose Readable Code Over Clever Code
An interview is not the place to compress five ideas into one unreadable loop. Clean code is easier to verify under pressure. Use descriptive variable names, keep helper functions small, and test with one or two examples out loud after writing the solution.
For a traversal problem, for instance, interviewers usually prefer a clear BFS implementation over a dense one-liner:
Complexity Is Part of the Answer
After coding, say the complexity explicitly. Do not assume the interviewer will infer it. If your solution uses recursion, mention the call-stack cost. If it sorts first, mention that the sort dominates runtime. If the answer is intentionally not optimal because of time, say what the next optimization would be.
This matters because interviewers are often testing judgment as much as implementation.
Common Pitfalls
- Jumping into code without clarifying constraints often leads to solving the wrong problem.
- Memorizing patterns without understanding when they apply causes mismatched solutions, such as using DFS where BFS is required.
- Ignoring the brute-force baseline makes it harder to explain why your optimized solution is actually an improvement.
- Writing clever but fragile code increases the chance of off-by-one errors and makes debugging harder in real time.
- Forgetting to discuss complexity leaves the solution incomplete from an interview perspective.
Summary
- Algorithm interviews evaluate reasoning, communication, and complexity tradeoffs, not just final code.
- Start by clarifying the problem and identifying the baseline solution.
- Move to a better data structure or algorithm only after you can explain why it improves the baseline.
- Prefer clear, testable code and always state time and space complexity explicitly.

