Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement a Basic Algorithmic Challenge

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

Your question is Implement a Basic Algorithmic Challenge. 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

TD Mobile alerts group consecutive transactions for analysis. Given a list of merchant identifiers in transaction order and an integer k, find the length of the longest contiguous transaction window containing at most k distinct merchants.

Use a sliding-window approach that runs efficiently for large transaction streams.

Formal Specification

Implement longest_alert_window(merchants, k).

  • Input: merchants, a list of strings representing merchant identifiers, and k, a non-negative integer.
  • Output: An integer representing the maximum length of a contiguous subarray with at most k distinct merchant identifiers.
  • Return 0 when k is 0 or the input list is empty.

Constraints

  • 0 <= len(merchants) <= 100,000
  • 0 <= k <= len(merchants)
  • Each merchant identifier is a non-empty string
  • The result must use contiguous transactions

Function Signature

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