Order Contract Sections So Defined Terms Precede Their Use
Last updated: November 29, 2026
Quick Overview
Each section of a contract defines some capitalized terms and uses others. Return an order of sections where every section appears after the section that defines each term it uses, preferring the original order, or report the sections involved in a circular definition.
Harvey
November 29, 20260
0
0 solved
Each section of a contract defines some capitalized terms and uses others. Return an order of sections where every section appears after the section that defines each term it uses, preferring the original order, or report the sections involved in a circular definition.
Definitions sections and cross references are a constant source of drafting errors, and Harvey builds tooling that checks and restructures legal documents. The interviewer wants to see you model this as a dependency graph, run Kahn's algorithm, keep the output stable, and give a useful error on cycles.
What the Interviewer Expects
- Map each term to its defining section, flagging duplicate definitions
- Build edges definer to user and skip self references
- Use Kahn's algorithm with a min heap to keep a stable order
- Detect cycles and name the sections involved
- Discuss what to do with used but undefined terms
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you report the exact cycle path rather than all stuck sections?
- What if a term may legitimately be defined in two places with the same meaning?
- How would you suggest the minimal number of moves to fix the current order?
- How would this run as you type in a draft?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: defines[section] and uses[section], both sets of terms. A term defined in two different sections is an error. Using a term with no definition is not an ordering constraint (it is a separate...
High-Level Design
- Build
definer[term] = section. - For each section
sand each used termt, add edgedefiner[t] -> s(dedupe edges, skip self use). - Kahn's algorithm with a min heap keyed by original posi...