VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 04: Sliding window

Lesson 4.10 of 10 in this module Unrolled Pro

Count N-grams in a Token Stream

An ML twin: count every run of n consecutive tokens in a stream and report the most frequent. From re-scanning the stream for each candidate, to slicing a window per position (O(L·n)), to a rolling hash that updates in O(1) per token, plus a heap for the top k. Then document boundaries, counts that don't fit in memory, sharded counting, and checking a training set for benchmark contamination.

about 10 min 4 Python examples 2 quick checks

Task. Given a list of token ids (a document or a stream) and n, count every n-gram (n consecutive tokens) and return the k most frequent, ties broken by the n-gram itself.

text
tokens = [5, 1, 2, 5, 1, 2, 5, 1], n = 2, k = 2
→ [((5, 1), 3), ((1, 2), 2)]      ((2, 5) also appears twice; ties break by value, (1, 2) < (2, 5))

ML context. N-gram counts are everywhere in language-model work: classic n-gram language models and BLEU/ROUGE scores, repetition detection and n-gram blocking in decoding (lesson 4.3), near-duplicate detection with shingles, and contamination checks: does the training data contain long n-grams (often 13-grams) from a benchmark? At the scale of a pretraining corpus, how you slide and hash the window decides whether the job takes an hour or a week.


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 "Count N-grams in a Token Stream"

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