Skip to content

Recognition guide

How to approach a coding problem you have never seen

Header art: a field of dim dots with one lit route from a mint start through lilac steps to a gold finish. A dotted ring circles the middle step.
One route from the start, through the middle step, to the finish.

Sooner or later the interviewer gives you a problem that is not on any list you practiced. Knowing more answers does not help in that moment. A repeatable routine does. This guide gives you one, in four steps: Understand, Think, Code, Explain. They are the same four steps an interviewer is watching for, so practicing them is practice for the room.

The routine does not need a flash of insight. Each step produces something you can say out loud.

Step 1: Understand

Do not touch the editor yet. The goal is to be sure you are solving the right problem.

  • Restate it in your own words. One sentence: "Given X, return Y."
  • Work one example by hand, and then make one of your own. If you cannot produce the answer for a small input, you do not understand the question yet.
  • Read the constraints. The size of the input tells you what is affordable. Around a thousand items allows O(n squared). Around a hundred thousand wants O(n log n) or better.
  • Ask about the edges: empty input, one item, duplicates, negative numbers, already sorted.

Step 2: Think

This is where most of the value is. Take it in this order.

  1. Say the brute force first, even if it is slow. "Try every pair, O(n squared)." It gives you a correct baseline and shows the interviewer you can solve it.
  2. Find the repeated work. Where does the brute force look at the same thing again? That repetition is what a pattern removes.
  3. Match the wording to a pattern. Use the table below as a starting point, then confirm with the counter-signals on the pattern's page.
  4. Check the idea on your example before you write any code. If the hand-run fails, the plan is wrong, and that is cheap to learn now.
  5. State the cost. Time and space, in one line.
If the statement saysTryRead
unsorted, "return the indexes", count, group, "seen before"hash mapHash Map, Hash map or sorting?
sorted, a pair, both ends, "in place"two pointersTwo pointers clues
contiguous slice, longest, shortest, "at most k"sliding windowRecognize a sliding window
sorted, a boundary, "smallest x such that"binary searchBinary search beyond sorted arrays
matching, "next greater", undostackStack clues
shortest path, level by level / every path, subtreebreadth-first / depth-firstDFS vs BFS

If nothing fits, that is fine. Go back to the brute force, find the repeated work, and ask what you could remember or sort to avoid it.

Step 3: Code

Now write, in small pieces.

  • Turn the plan into comments and fill them in. Each comment is one step of your Think notes.
  • Run your example by hand against the finished code, not against what you meant to write.
  • Test the edges you listed in step 1.
  • When it breaks, form one guess about why and test that guess. Changing three things at once hides the bug.

Step 4: Explain

You are not done when the tests pass. Say it back.

  • The idea in two sentences, with no code words: what you keep track of and why that is enough.
  • The cost, with the reason: "one pass, and each lookup is constant, so O(n) time and O(n) space for the map."
  • The trade-off. Name one alternative and what it gives up: "Sorting first would use less memory but lose the original indexes."

When you are stuck

Do not stay silent. Try these in order.

  1. Shrink the input to two or three items and solve it by hand. Watch what you did.
  2. Solve an easier version: drop one constraint, then add it back.
  3. Say what the brute force repeats, and what you could store to skip it.
  4. Ask a specific question aloud. Interviewers often hint when they can see your reasoning.

Counter-signals

These feel like good moves and are not.

  • A keyword is not proof. "Sorted" suggests binary search or two pointers, but check that the statement actually needs them.
  • A slow, correct answer beats a fast, wrong one. Get the brute force down first if you are unsure. Optimize after.
  • A familiar title is not the same problem. A problem that looks like one you know may change one condition that breaks the old answer.

Checklist

  1. Can I restate the problem and solve a small example by hand?
  2. Do I know the constraints and the edge cases?
  3. Have I said the brute force and its cost?
  4. Do I know what work it repeats?
  5. Have I named a pattern or a reason for none, and checked it on the example?
  6. Did I state time and space before coding?
  7. Can I explain it in two sentences and name one alternative?

Three short walk-throughs

  1. 1. "Return the length of the longest substring with no repeated characters."

    Understand: the input is a string and the answer is a length. Think: brute force checks every substring, which re-reads the same characters again and again. "Contiguous" and "longest" point to a sliding window with a set of what is inside. Code: grow the right end, shrink the left end while a repeat is present, track the best length. Explain: each end moves forward once, so O(n).

  2. 2. "Find the first character in a string that appears only once."

    Understand: "first" means original order matters. Think: brute force counts each character by scanning, O(n squared). Counting is the repeated work, so one pass builds a map of counts. A second pass reads the string in order and returns the first with count one. Code: two loops. Explain: O(n) time, and the map holds at most one entry per distinct character.

  3. 3. "Return the smallest capacity so all packages ship within d days."

    Understand: the answer is a number, not a plan. Think: brute force tries every capacity. "Smallest" and "within d days" mean more capacity never hurts, so the yes-or-no answers flip once. That is binary search on the answer. Code: write a check that counts days for a capacity, then search the range. Explain: about log of the range checks, each one pass over the packages.

Common traps

  • Coding before understanding. The most common reason a solution is wrong is that it solves a different question.
  • Skipping the hand-run. A plan that fails on a three-item example will not pass hidden tests.
  • Jumping to a pattern name. Naming it too early locks in an idea you have not checked.

Quick answers

What if I cannot recognize any pattern?

Use the brute force and look for repeated work. Often it points to a map, a sort, or two indexes. A correct slow answer, clearly explained, is a good result.

How long should each step take?

There is no fixed split, but Understand and Think deserve more time than most people give them. Start coding once you can describe the plan in a few sentences.

Is it okay to ask for hints?

Yes. Showing how you use a hint is part of the interview. Ask a specific question, and then act on the answer.

Practice it on Algofily

Recognizing the pattern is a skill on its own, so Algofily trains it before you write code. Each problem moves through seven stages: Understand, Pattern, Think, Code, Tests, Debug, and Explain. In the Code stage, Algofily AI can give progressive hints (sign-in required).

  • Browse every pattern on the Patterns page, and read the matching recognition guide for the ones you find hardest.
  • Start with the Hash Map or Two Pointers pattern page if you are not sure where to begin.

While you code, Algofily AI can ask a question and open the next hint.