Look-alikes: the right pattern for the wrong prompt

Every pattern buys its speed with one assumption about the input. A look-alike is a prompt that reads like the pattern’s home and quietly breaks it.

Why do I keep reaching for the wrong pattern?

Because the prompts rhyme. Two problems can share every word that matters to you (subarray, sum, shortest, order) and differ in one clause you read past: a value may be negative, an edge carries a weight, the answer has to be listed rather than counted. Patterns are taught by their shape, so the shape is what you match on, and the shape is exactly what a look-alike copies. The repair is to learn each pattern by its assumption instead, because the assumption is what the wrong prompt breaks.

What is the fastest way to tell a sliding window from prefix sums?

Ask whether a value can be negative. A window works because growing it moves the running sum in one direction only, so once the window is illegal you can shrink from the left and never look back. One negative number makes that false: a window that was over the limit can come back under it two steps later. When negatives are allowed and the target is exact, keep every running total in a hash map and look up the total you need. When the target is an inequality, keep the totals in order instead, because an inequality is not a lookup.

Is a look-alike always a wrong answer, or is it sometimes only slow?

Both happen, and the slow ones are harder to catch. Most of the failures on this page are wrong answers the build recorded: the code returns a number, the number is not the answer, and nothing raises an exception. Three are correct and still lose you the round. Selection sort on a linked list is in place and quadratic. A recursion that counts combinations instead of listing them is right and exponential. A trie asked only about whole words is right and allocates a node per character where a set would hash once per word. Say the cost out loud when you rule one of those out.

How do I check the assumption in the room without burning time?

Sixty seconds, before you type. Read the constraint line for three things: whether a value can be negative, whether the items are sorted, and whether anything carries a weight or a cost. Then say the assumption your pattern needs as one sentence, and try to break it with three or four hand-picked items. If you cannot break it, say so with the reason and start coding. If you can, you have found the counterexample and the right pattern in the same minute.

Which pairs should I drill first?

The ones a single clause decides, because those are the ones you can catch by reading. Sliding window against prefix sums turns on a negative value. Two pointers against a hash map turns on whether the input is sorted. BFS against Dijkstra turns on whether an edge carries a weight, and Dijkstra against Bellman-Ford on whether a weight can be below zero. Backtracking against dynamic programming turns on whether the prompt wants the candidates listed or only counted. Five clauses, ten patterns, and most of the damage.