Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Coding Benchmark Problem Solving

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

Your question is Coding Benchmark Problem Solving. 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

Maya wants to identify the longest uninterrupted sequence of wallet activity containing no more than k distinct event types. Given a list of event type strings in chronological order, return the inclusive start and end indices of the longest contiguous window with at most k distinct types. If multiple windows have the same maximum length, return the one with the smallest start index.

Formal Specification

Implement longest_activity_window(events, k), where events is a list of strings and k is a positive integer. Return a two-element list [start, end]. If no valid window exists, return [-1, -1]. The empty input also returns [-1, -1].

Example 1

Input: events = ["send", "cash_in", "send", "pay", "pay"], k = 2

Output: [2, 4]

Explanation: The window ["send", "pay", "pay"] has two distinct types and length three, which is maximal.

Example 2

Input: events = ["pay", "cash_in", "send"], k = 1

Output: [0, 0]

Explanation: Every event type is different, so the longest valid window contains one event. The earliest such window starts at index zero.

Constraints

  • 0 <= len(events) <= 10^5
  • 1 <= k <= 10^5
  • Each event type is a non-empty string of at most 30 characters
  • The result uses inclusive zero-based indices
  • The input list is not reordered

Function Signature

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