Find the Lowest Shared Heading of Two Contract Clauses

Last updated: November 29, 2026

Quick Overview

A contract outline is a tree where each clause id maps to its parent heading id (the root maps to None). Given two clause ids, return the deepest heading that contains both.

Harvey
Coding & Algorithms
Software Engineer
Harvey
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


A contract outline is a tree where each clause id maps to its parent heading id (the root maps to None). Given two clause ids, return the deepest heading that contains both.

Harvey's drafting and review tools reason about document structure, for example to show which article two flagged clauses live under. This is a warm-up tree question that tests lowest common ancestor with parent pointers and clean handling of the case where one node is an ancestor of the other.

What the Interviewer Expects
  • Recognize lowest common ancestor with parent pointers
  • Equalize depths before walking up together
  • Handle one clause being an ancestor of the other
  • Analyze O(h) time and O(1) extra space
  • Mention the set-based alternative and its trade-off
Key Topics to Cover
trees
lowest common ancestor
parent pointers
depth
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 answer many such queries quickly on a large document?
  • What if the input is a child list instead of parent pointers?
  • How do you handle a clause id that is not in the outline?
  • Return the path between the two clauses, not just the ancestor.
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

Input: parent dict from clause id to parent id, root maps to None; two ids a and b that exist in the same tree. Output the lowest node that is an ancestor of both, where a node counts as its own...

High-Level Design

Compute the depth of each node by walking to the root. Move the deeper node up until both are at the same depth. Then step both up together until they meet. An alternative is to put all ancestors of `...


Submit Your Answer
Markdown supported

Related Questions