Map Cited Character Spans to Per-Page PDF Highlights

Last updated: November 29, 2026

Quick Overview

An answer cites a list of [start, end) character spans in a document whose text is the concatenation of its pages. Given the page start offsets, merge overlapping or touching spans and return highlight ranges as (page, start, end) with page-relative offsets, splitting any span that crosses a page break.

Legora
Coding & Algorithms
Software Engineer
Legora
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


An answer cites a list of [start, end) character spans in a document whose text is the concatenation of its pages. Given the page start offsets, merge overlapping or touching spans and return highlight ranges as (page, start, end) with page-relative offsets, splitting any span that crosses a page break.

Every Legora answer, whether in the Assistant or a Tabular Review cell, links back to the exact source passage, so the viewer must turn model citations into highlights on PDF pages. The interviewer looks for interval merging, binary search over page offsets, and careful boundary handling.

What the Interviewer Expects
  • Clamps spans to the document and drops empty ones
  • Sorts and merges overlapping and adjacent spans in one pass
  • Uses binary search to find the starting page instead of a linear scan
  • Splits spans correctly across one or many page boundaries
  • Returns page-relative offsets with 1-based page numbers
  • Analyses complexity as O(k log k + k log p + output)
Key Topics to Cover
interval merging
binary search
offset mapping
citations
PDF rendering
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 also return the bounding boxes for each highlight given per-character coordinates?
  • What changes if the model's spans are off by a few characters because of whitespace normalisation?
  • How would you merge citations from several different answers while keeping track of which answer each highlight came from?
  • How would you handle scanned pages where OCR text order differs from the visual order?
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

Inputs: page_starts sorted with page_starts[0] == 0, total doc_len, and spans [start, end). Output: non-overlapping highlights (page, start, end) in reading order, offsets relative to the pa...

High-Level Design
  1. Clamp and drop empty spans. 2. Sort and merge. 3. For each merged span, bisect_right(page_starts, s) - 1 gives the page; walk forward, cutting at each page end until the span is exhausted....

Submit Your Answer
Markdown supported

Related Questions