Find Caption Spans Made of Every Flagged Token Back to Back
Last updated: November 29, 2026
Quick Overview
Given a caption string s and a list of equal-length flagged tokens (duplicates allowed), return every start index where s contains a concatenation of all the tokens, each used exactly as many times as listed, in any order and with nothing in between.
Meta
November 29, 20260
0
0 solved
Given a caption string s and a list of equal-length flagged tokens (duplicates allowed), return every start index where s contains a concatenation of all the tokens, each used exactly as many times as listed, in any order and with nothing in between.
A Hard sliding window over words rather than characters, framed around detecting obfuscated policy-violating phrases. Meta interviewers look for the per-offset window that steps by the token length and resets on unknown tokens.
What the Interviewer Expects
- Notices that equal token length lets you step by word length
- Runs one sliding window per offset from 0 to word length - 1
- Maintains a token count map and shrinks when a token is over-used
- Resets the window when it hits a token that is not flagged
- Gives O(n * w) time where w is the 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
- Tokens can have different lengths. How does the approach change?
- Return just the count of matching spans.
- Allow at most one unflagged token in the span.
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: s (up to 10^4 characters) and tokens, a non-empty list of strings all of length w. Output: sorted start indices where a span of length len(tokens) * w is a permutation of the token multis...
High-Level Design
Any valid span starts at an offset congruent to some r in [0, w). For each r, walk s in steps of w, reading one token at a time. Keep have[token] and the number of tokens in the window. If t...