Find the Shortest Passage Containing Every Query Term

Last updated: November 29, 2026

Quick Overview

Given a tokenized document and a list of query terms (with possible repeats), return the start and end token indices of the shortest contiguous passage that contains every term at least as many times as it appears in the query.

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

0

0

0 solved


Given a tokenized document and a list of query terms (with possible repeats), return the start and end token indices of the shortest contiguous passage that contains every term at least as many times as it appears in the query.

When Harvey shows a lawyer where an answer came from, it highlights a tight span of the source rather than a whole page. This classic sliding window question tests whether you can maintain counts with multiplicity, shrink correctly, and keep the invariant clear.

What the Interviewer Expects
  • Use a variable sliding window with a need counter
  • Track a single 'missing' integer rather than rescanning counts
  • Handle repeated query terms (multiplicity)
  • Normalize case consistently
  • Return None when no window exists
Key Topics to Cover
sliding window
two pointers
hash map counting
text highlighting
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 allow fuzzy matches such as stems or synonyms?
  • What if you want the top 3 non-overlapping passages?
  • How would you do this over an inverted index of positions instead of scanning tokens?
  • How do you prefer passages that do not cross a sentence or clause boundary?
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: a list of tokens and a list of query terms. Matching is case-insensitive. If a term appears twice in the query, the passage must contain it twice. Return (start, end) inclusive, or None. If ...

High-Level Design

Expand right across the tokens. Keep have counts and a missing counter equal to the number of required occurrences still unmet. When missing hits 0, the window is valid: record it, then advanc...


Submit Your Answer
Markdown supported