Backtracking interview questions
Choose, explore, undo: listing selections and orderings, searching grids and boards, and pruning the search so exponential work stays small in practice.
– interview-ready · 12 questions in this topic
– of 12 tried
Saved on this device. Interview-ready means you got it right on two different days. Guesses and hints count for less.
Questions
- Count the picks that hit an exact totalPredict
- Every order of a playlistPredict
- Every selection from a set of toppingsPredict
- Every team of k from n playersFill the blank
- Every balanced string of n bracket pairsPredict
- Trace a word through a letter gridPick the fix
- Cut a word into pieces that each read the same backwardsPredict
- Count the ways to place queens that cannot attackPredict
Practise this topic
Flashcards · 22
Flip, then say if you knew it. Needs JavaScript; every answer is also on its question page.
Quick quiz · 12 questions
One exercise per question, checked as you go. Needs JavaScript.
Mock interview · 2 sets
Timed, out loud, then tick what you covered. Needs JavaScript.
Quick facts
Recall the answer first, then tap Show to check.
itertools.permutationspermutations(xs)yields orderings in lexicographic order of positions, so sorted input gives sorted output.itertools.combinationscombinations(xs, k)yields every k-item selection in input order without repeats; there are n! / (k! (n - k)!) of them.itertools.productproduct("ab", repeat=3)yields all 2³ strings, the same as three nested loops.Copying the path
out.append(path[:])stores a snapshot; appendingpathitself stores a reference that later changes.sys.setrecursionlimitBacktracking depth equals the number of choices in a path; it rarely nears CPython's default limit of 1000.
Printable cheat sheet · Review your weak spots in this topic
Share your progress
Sources: Topic outline informed by the Tech Interview Handbook by Yangshun Tay and the NeetCode 150 pattern grouping by neetcode-gh, both MIT licensed. Every problem, solution and explanation is written by Best Latest Apps. Credits and license