Data and Knowledge SystemsKnowledge representation and evidenceStaff
How would you reconstruct reading order from a PDF page whose column layout changes between sections?
By Amit Kumar4 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A research PDF has embedded text and bounding boxes, so optical character recognition (OCR) is unnecessary. An English page starts with a full-width heading and two columns, then has another full-width heading followed by a new two-column section. Global top-to-bottom sorting interleaves columns, while sorting the entire left column first moves the second heading into the wrong place. Design a block-ordering algorithm and chunking contract, work through a small coordinate example, and explain how to handle captions, footnotes and ambiguous layouts.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
A PDF can draw correctly positioned text without storing the sequence in which a person should read it. The extraction problem here is to turn positioned blocks into an ordered document while preserving section and paragraph relationships. Accurate embedded text removes the need to recognize characters from an image, but it does not solve reading order. PyMuPDF's text extraction recipes explain why a plain extraction can differ from natural reading sequence. Its FAQ is useful background, rather than a guarantee that one sorting option handles every layout.
Make the coordinate system explicit before designing the sorter. In this hypothetical example, a page is 1,000 units wide, x increases rightward and y increases downward from its top edge. Each block has a rectangle, text, page number and stable identifier. Normalize rotations and the extractor's coordinate convention into this representation. Otherwise an algorithm that works on one parser can reverse rows or misclassify columns when another parser reports a different origin.
Suppose heading H1 spans x=80..920 at y=80. Paragraphs A1 and A2 occupy x=80..460 at y=180 and y=300. Paragraphs B1 and B2 occupy x=540..920 at the same respective heights. Heading H2 spans x=80..920 at y=600. Below it, C occupies the left column and D the right at y=700. Assume the headings and paragraph roles have been identified correctly and each paragraph is already a complete block. The intended sequence is H1, A1, A2, B1, B2, H2, C, D. Sorting by y and then x instead yields H1, A1, B1, A2, B2, H2, C, D. Sorting by x over the entire page can move C ahead of B1 and detach H2 from its section.
Use the headings to divide this page into section regions, then infer columns within each region. The empty horizontal gap between x=460 and x=540 is evidence of a gutter separating the two columns in the first section. For these English body paragraphs, order blocks downward within the left column, then continue at the top of the right. End that region before H2 and repeat the decision for the lower section. Do not carry the first section's column assignment blindly across the rest of the page or into the next page.
An implementation can express this as a graph of precedence constraints. Nodes are blocks. Edges say which block must precede another, such as H1 before A1, A1 before A2, A2 before B1, B1 before B2 and B2 before H2. A topological traversal produces an order consistent with those constraints. This representation also retains why an ordering was chosen. If inferred relationships create a cycle, or leave several meaningfully different reading paths, the system should flag the region for review. Adding an arbitrary coordinate tie-breaker makes output deterministic but does not make it correct.
The example deliberately supplies unambiguous headings. A production parser still has to identify their roles. Font size, alignment, whitespace and repeated styling can help distinguish headings from other spanning objects. A full-width figure is not automatically a section boundary. A caption should retain its figure association, and a footnote should keep its reference relationship even if it appears at the bottom of the page. Preserve table cells and their header hierarchy as a structured object rather than inserting them as an arbitrary sequence of nearby body lines. Language direction and document conventions also matter. Left-column-first is an assumption for this example, not a universal reading rule.
Emit more than one flattened string. Pass the chunker an ordered sequence with section identity, block roles, explicit paragraph boundaries and links to captions or notes. Keep source revision and bounding boxes for citations. Permit continuation across columns or pages only when the document structure supports it. A token-limited chunk may split a long paragraph, but it must not manufacture a new paragraph by taking alternating lines from two columns. Nor should it append an unrelated sidebar because the sidebar happened to be the next geometric object.
Validate the algorithm using region-level expected orders and prohibited joins. The coordinate example catches both global sorting errors. Add single-column pages, a heading partway down a page, a spanning figure, footnotes, and a paragraph that genuinely continues onto the next page. Inspect the rendered page alongside numbered block overlays. A high text-coverage score proves that words survived, not that the ordering is useful. Pairwise ordering checks help locate errors, while answer-support checks expose cases where a small order change alters a scientific claim.
Geometry alone cannot always distinguish a sidebar from a second article or establish which result a detached limitation qualifies. Preserve the graph and uncertainty, then use a document-specific rule, visual review or abstention for those cases. A more expensive layout model is justified only if it improves the hard examples under the same criteria. The OCR pipeline investigation instead locates the stage that corrupted clause identity. This question assumes the blocks exist and asks how to order and represent them without introducing a false relationship.
Related questions
Keep practising data and knowledge systems
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux