DSA · Patterns, not puzzles

How do you approach a coding problem in an interview?

Easyinterviewsproblem-solvingcommunicationUpdated Oct 2026
Quiz this track

Short answer

Clarify the input and constraints, restate the problem, work through a small example, choose a pattern with a complexity argument, then code and test. Communicate the plan before writing.

The interview is a collaboration, not a race to type. A structured approach makes your reasoning visible and gives the interviewer chances to redirect you before you spend time on the wrong solution. Silence is the enemy; narrate the decisions.

A reliable sequence

  • Clarify: input types, size, duplicates, ordering, mutation, output format
  • Restate: say the problem back in one sentence
  • Example: walk through a small case and an edge case
  • Plan: name the pattern and justify it with complexity
  • Code: write readable code with meaningful names
  • Test: trace the example, then try edge cases
js
function twoSumSorted(values, target) {
  let left = 0;
  let right = values.length - 1;
  while (left < right) {
    const sum = values[left] + values[right];
    if (sum === target) return [left, right];
    if (sum < target) left += 1;
    else right -= 1;
  }
  return [];
}
A sorted input plus a pair requirement suggests two pointers, which gives O(n) time and O(1) extra space.

If you get stuck, reduce the problem. Solve a brute-force version, then look for repeated work or a monotonic property that allows a better pattern. Saying the brute force and its complexity is better than saying nothing.

Do not jump to code before confirming the output format. Returning indices when the interviewer wanted values is a wasted solution, no matter how efficient it is.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What do you do when you do not recognize the pattern?
  2. 2How do you decide between brute force and optimization?
  3. 3How do you test your code without running it?
  4. 4What do you say when you are stuck?

Say it in the interview

I start by clarifying the input, constraints and output, then restate the problem and walk through a small example. I name the pattern I plan to use and justify its complexity before coding. After writing, I trace the example and test edge cases. If I get stuck, I describe a brute-force solution first, then look for repeated work or monotonic structure that lets me improve it.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live DSA mock interview and get scored feedback on depth, clarity and confidence.