How do you recognise a familiar pattern in a new interview question?
Look at the shape of the problem rather than its wording. Ask what the input is, what the output is, and what operations are allowed. Most questions are one of a small number of shapes wearing different clothes, and the shape tells you the technique before you have written any code.
def has_duplicates(values):
seen = set()
for value in values:
if value in seen:
return True
seen.add(value)
return False
print(has_duplicates([1, 2, 3, 2]))
print(has_duplicates([1, 2, 3]))The outputs are True and False. The question here might be phrased as detecting a repeated ticket number, or as checking whether a set of identifiers is unique, and the underlying technique is the same either way.
The mechanism is a checklist of structural clues. If the question asks what exists, given a set of things, use a hash set. If it asks about a contiguous range, think sliding window. If it asks for the best answer over an order that must be preserved, think dynamic programming. If the answer depends on a search space with costs, think graph search. If it asks for the best choice that cannot be undone, think greedy.
The trade-off to name is that the checklist gives you a starting hypothesis, not a guarantee. The value is in how quickly you can write the first few lines and test them against a small example, because a wrong pattern hypothesis is discovered in thirty seconds rather than twenty minutes. The other habit worth building is saying your hypothesis out loud, since the interviewer can correct you cheaply then and would rather correct you than let you build the wrong solution.