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
Queue Data Structure Implementation
00:00
5 left

Queue Data Structure Implementation

EasyPython

Problem

Gro Intelligence processes ordered work items such as data refresh requests and alert evaluations. Implement a queue using two stacks, while preserving first-in, first-out behavior and achieving amortized O(1) time for each operation.

Write implement_queue(operations). Each operation is represented as an array. Supported operations are:

  1. ["enqueue", value]: Add value to the back of the queue. This operation produces no output.
  2. ["dequeue"]: Remove and return the front value. The input will not dequeue from an empty queue.
  3. ["peek"]: Return the front value without removing it. The input will not peek at an empty queue.
  4. ["size"]: Return the current number of queued values.

Return an array containing the results of dequeue, peek, and size operations in their original order. Do not include results for enqueue operations.

Constraints

  • 1 <= operations.length <= 10^5
  • Each operation is valid and uses the specified format
  • Values may be integers, strings, booleans, or null
  • At most 10^5 values are held by the queue at once
  • dequeue and peek are never called when the queue is empty

Function Signature

def implement_queue(operations):
Interviewer

Your question is Queue Data Structure Implementation. 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.