Find Every Position Where a Query's Tokens Appear Concatenated

Last updated: November 29, 2026

Quick Overview

Given a document string and a list of query tokens that all have the same length, return every start index where some ordering of all the tokens appears back to back with nothing in between.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a document string and a list of query tokens that all have the same length, return every start index where some ordering of all the tokens appears back to back with nothing in between.

A Google onsite Hard string problem framed around phrase matching in a search index. The interviewer checks a word-level sliding window run once per offset modulo the token length, and correct handling of repeated tokens.

What the Interviewer Expects
  • Runs one window per offset from 0 to token length minus 1
  • Moves in whole-token steps
  • Keeps a token count map and shrinks on excess
  • Resets the window on an unknown token
  • States O(n * L) time where L is token length
Key Topics to Cover
Sliding Window
Hash Map
Strings
Multiset Matching
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
  • What if tokens can have different lengths?
  • How would you use rolling hashes to compare tokens in O(1)?
  • How would you run this over millions of documents?
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
  • Input: string doc, list tokens of equal length L (may repeat).
  • Output: sorted list of start indices where the concatenation of a permutation of tokens begins....
High-Level Design

Every match starts at some index with remainder r = start % L, and inside a match the tokens sit at start, start + L, .... So for each r in [0, L) we slide a window over the token sequence `do...


Submit Your Answer
Markdown supported