Renumber Contract Clauses and Rewrite Cross-References After Deletion

Last updated: November 29, 2026

Quick Overview

A contract is a list of clauses with nesting levels, numbered hierarchically (1, 1.1, 1.1.1). When some clauses are deleted (with their sub-clauses), renumber the survivors and rewrite every 'Clause X.Y' reference in the text to the new number, marking references to deleted clauses.

Legora
Coding & Algorithms
Software Engineer
Legora
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A contract is a list of clauses with nesting levels, numbered hierarchically (1, 1.1, 1.1.1). When some clauses are deleted (with their sub-clauses), renumber the survivors and rewrite every 'Clause X.Y' reference in the text to the new number, marking references to deleted clauses.

Drafting agents that edit agreements, like Legora's Workflows and Word add-in, break contracts if they delete a clause and leave stale cross-references. The interviewer is checking tree-shaped numbering over a flat list, subtree deletion, and the classic trap of chained string replacements.

What the Interviewer Expects
  • Derives hierarchical numbers from levels with a counter stack
  • Deletes whole subtrees, not only the named clause
  • Builds an old-number to new-number map keyed through stable clause ids
  • Rewrites references in a single regex pass so 3.2 to 3.1 does not then become 2.1
  • Marks references to deleted clauses instead of silently dropping them
  • Leaves unknown references alone and can flag them
Key Topics to Cover
hierarchical numbering
stack
regex substitution
tree deletion
document editing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you handle range references such as 'Clauses 4.1 to 4.3' when 4.2 is deleted?
  • How would you support Schedules, lettered sub-clauses (a), (b) and roman numerals?
  • How would you apply the same rewrite to a .docx with Word's auto-numbering fields instead of plain text?
  • How would you test this so a lawyer trusts it on a 120 clause agreement?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Each clause has a stable id, a level (1 is top) and text. Deleting a clause removes it and every following clause with a deeper level until we return to its level. References look like `Clause 3...

High-Level Design

Numbers are derived, ids are identity. 1) Expand deletions to subtrees in one forward pass. 2) Number the original outline and the kept outline with a counter stack. 3) Map old number to id to new num...


Submit Your Answer
Markdown supported

Related Questions