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
Insert Element in a Heap
00:00
5 left

Insert Element in a Heap

EasyPython

Problem

Blue Yonder fulfillment workloads prioritize jobs by the smallest priority value. Implement insertion into an indexed binary min-heap that stores jobs as [priority, sequence, job_id]. The heap must preserve lexicographic ordering by priority, then sequence, then job ID.

In addition to the heap array, maintain a position map so positions[job_id] always equals the job's current heap index. The input heap is valid, job_id is not already present, and both the heap and position map must be updated in place. Return [heap, positions] after insertion.

Formal Specification

Implement insert_job(heap, positions, job_id, priority, sequence).

  • heap: a list of three-element lists, where each entry is [priority, sequence, job_id].
  • positions: a dictionary mapping each existing job ID to its index in heap.
  • job_id: the unique string identifier for the new job.
  • priority: an integer priority, with smaller values processed first.
  • sequence: a unique integer used to preserve arrival order when priorities match.
  • Return the updated heap and position map as [heap, positions].

After insertion, every parent must be less than or equal to its children under the specified lexicographic ordering, and every job must map to its correct index.

Constraints

  • 0 <= len(heap) <= 10^5
  • -10^9 <= priority <= 10^9
  • 0 <= sequence <= 10^9
  • All job IDs are unique strings
  • Every input heap is a valid indexed min-heap

Function Signature

def insert_job(heap, positions, job_id, priority, sequence):
Interviewer

Your question is Insert Element in a Heap. 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.