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.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample Answer
Requirements
- Input: string
doc, listtokensof equal lengthL(may repeat). - Output: sorted list of start indices where the concatenation of a permutation of
tokensbegins....
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...