VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 03: Two pointers

Lesson 3.9 of 10 in this module Unrolled Pro

Merge Two Sorted Prediction Lists

An ML twin of Merge Sorted Array: two retrieval shards each return results sorted by score; produce the global top-k with each document once. From concatenate-dedupe-sort, to a two-pointer merge that stops after k results, to many shards with a heap, and what to do when the two models' scores aren't comparable (normalise, or fuse ranks).

about 10 min 4 Python examples 2 quick checks

Task. Two retrieval shards (or two models) each return a list of (score, doc_id) pairs, sorted by score, highest first. Return the global top k as a list of doc ids:

  • each document at most once, using its highest score if both lists contain it;
  • ties broken by doc_id (ascending) so the output is deterministic.
text
a = [(0.92, "d3"), (0.81, "d7"), (0.40, "d1")]
b = [(0.88, "d5"), (0.81, "d2"), (0.75, "d7"), (0.10, "d9")]
k = 4   →   ["d3", "d5", "d2", "d7"]

ML context. This is the last step of every sharded search: a vector index split across machines, a keyword engine and a vector engine, or several candidate generators in a recommender. Each returns its best results; the aggregator merges them. Doing it in O(k) instead of sorting everything matters when each shard returns thousands of candidates and the request budget is a few milliseconds.


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 Two Sorted Prediction 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