Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Coding Benchmark Problem Solving
00:00
5 left

Coding Benchmark Problem Solving

MediumPython

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):
Interviewer

Your question is Coding Benchmark Problem Solving. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.