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
Promise Timeout With Maps
00:00
5 left

Promise Timeout With Maps

HardPython

Problem

Grammarly Editor may have many asynchronous requests in flight at once. Implement an event processor that tracks each request in a map and marks it as completed, timed out, or still pending without scanning every active request for each event.

Each request has a unique identifier. A start event begins a request, and a resolve event represents its promise resolving. A request times out when it remains unresolved beyond timeout_ms. A resolution occurring exactly at its deadline succeeds.

Formal Specification

Implement process_request_timeouts(events, timeout_ms, end_time).

  • events is a list of dictionaries sorted by nondecreasing time. Each dictionary has integer time, string type ("start" or "resolve"), and string id.
  • timeout_ms, end_time, and event times are nonnegative integers.
  • Return a dictionary mapping every started request ID to "completed", "timed_out", or "pending".
  • At end_time, unresolved requests whose deadline is at or before end_time are timed out. Later deadlines remain pending.

Use a hash map for active requests and a min-heap to find the next deadline efficiently.

Constraints

  • 1 <= len(events) <= 10^5
  • Each request starts at most once and resolves at most once.
  • Every resolve event refers to a started request.
  • 0 <= event.time <= end_time <= 10^9
  • Events are sorted by nondecreasing time.

Function Signature

def process_request_timeouts(events, timeout_ms, end_time):
Interviewer

Your question is Promise Timeout With Maps. 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.