Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Real-Time Anagram Detection

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Real-Time Anagram Detection. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Amazon Kinesis can deliver text as a sequence of chunks rather than one complete string. Given an iterable of lowercase English text chunks and a lowercase target word, find every starting character index where a contiguous window is an anagram of the target.

Process chunks in order without concatenating the entire stream. Windows may begin in one chunk and end in another. Return starting indices in the complete logical stream, ordered from left to right.

Formal Specification

Implement find_stream_anagrams(stream, target), where stream is an iterable of strings containing only lowercase letters and target is a non-empty lowercase string. Return a list of integers. Each integer is the zero-based starting index of a window whose length equals len(target) and whose character frequencies exactly match the target frequencies.

Use a fixed-size frequency array for the 26 lowercase letters. The implementation should retain only the current window, not the entire stream.

Constraints

  • 1 <= len(target) <= 100,000
  • The total number of streamed characters is at most 1,000,000
  • Each chunk contains only lowercase English letters
  • The stream may contain empty chunks
  • The target contains only lowercase English letters

Function Signature

def find_stream_anagrams(stream, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output