Pick the Shortest Snippet Window Covering All Query Terms

Last updated: November 29, 2026

Quick Overview

Given a document as a list of tokens and a list of query terms, return the start and end indices of the shortest contiguous window that contains every query term that appears anywhere in the document, or None if no term appears.

Glean
Coding & Algorithms
Software Engineer
Glean
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a document as a list of tokens and a list of query terms, return the start and end indices of the shortest contiguous window that contains every query term that appears anywhere in the document, or None if no term appears.

Choosing which part of a long Confluence page or Drive doc to show under a Glean result is a core snippet-generation problem. Interviewers use it to see whether a candidate reaches the sliding-window technique and handles the realistic wrinkle that not every query term occurs in the document.

What the Interviewer Expects
  • Restricts the required set to query terms actually present, otherwise no window exists
  • Uses a two-pointer sliding window with a count map, not brute force over all windows
  • Shrinks from the left while the window still covers every needed term
  • Handles repeated terms and case folding correctly
  • Explains O(n) time and O(q) space
Key Topics to Cover
sliding window
two pointers
hash map counting
snippet generation
positional index
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 weight rare terms more than common ones when not all terms fit in a 30-token window?
  • How would you return the best window from positional postings without scanning the whole document?
  • How should sentence boundaries influence the chosen window?
  • What changes if the query contains synonyms that should count as the same term?
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

Tokens are already normalized words. Query terms may repeat and may be missing from the document. The window must contain each present term at least once; return (start, end) inclusive, preferring t...

High-Level Design

This is minimum window covering a set. Compute need = query terms intersected with document tokens. Expand right across tokens, counting needed terms in have; covered tracks how many distinct ...


Submit Your Answer
Markdown supported

Related Questions