VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 09: Heaps and top-k

Lesson 9.11 of 12 in this module Unrolled Pro

Merge k Sorted Posting Lists

Answer keyword queries over an inverted index, where each term maps to a sorted list of document ids. From converting every list to a set and sorting the union or intersection (O(N log N)), to a heap merge that emits the union in order while dropping duplicates (O(N log k)), to an intersection driven by the shortest list with galloping search in the others. Then ranked OR queries that sum term scores and keep the top K with a heap, delta-encoded postings decoded lazily, and phrase queries.

about 10 min 4 Python examples 2 quick checks

Problem. An inverted index maps each term to its posting list: the sorted ids of the documents containing it.

text
"attention"  →  [2, 5, 9, 14, 20, 31]
"kv"         →  [5, 14, 31, 40]
"cache"      →  [1, 5, 14, 31, 33]

Answer two kinds of query over k terms:

  • OR (any term): the union, in sorted order, each id once → [1, 2, 5, 9, 14, 20, 31, 33, 40]
  • AND (all terms): the intersection → [5, 14, 31]

Posting lists for common terms hold millions of ids, so the cost depends on how much of each list you must touch.

Why it matters. Keyword search (BM25) still sits in front of or beside vector search in most RAG systems ("hybrid retrieval"), and it's built on exactly these merges. Search engines, log search (find every request with model=x AND status=500) and dataset filtering all run AND / OR over sorted id lists. The two operations need different tools: a heap for the union, the shortest list for the intersection.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Merge k Sorted Posting Lists"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee