Sequence Reconstruction
Problem Statement
You get nums, a list that holds each value from 1 to n exactly once (so it is a permutation of 1..n). You also get sequences, a list of short lists of values.
A list fits when every sequence appears inside it as a subsequence: its values show up in the same order, though not necessarily next to each other.
Return true only when nums is the only list of length n that fits all the sequences. Return false otherwise. That includes the case where several lists fit, and the case where nums itself does not fit.
nums: a permutation of 1..n. sequences: a list of lists of values, each value between 1 and n.
true if nums is the only list that fits every sequence, otherwise false.
1 comes before 2, and 2 comes before 3. Only the list 1, 2, 3 fits.
2 and 3 can swap. Both [1, 2, 3] and [1, 3, 2] fit, so nums is not the only one.
The neighbouring pairs chain together as 4 → 1 → 5 → 2 → 6 → 3. That chain is the only list that fits.
The two sequences contradict each other: 1 must come before 2 and 2 before 1. No list fits.
- • nums is a permutation of the values 1 to n, where n = nums.length.
- • Every value in sequences is between 1 and n.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Try every order of 1..n; keep those where all sequences fit; accept only one that equals nums
Arrows from neighbouring pairs; Kahn must have exactly one free value each step and match nums
Any list that fits must be an order of the values 1..n. Try all of them. Keep each order in which every sequence is a subsequence. Return true only when exactly one order is kept and it equals nums.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Topological Sort translate into production architectures and low-level memory mechanics.