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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Thread-Safe Shared Resource Manager
00:00
5 left

Thread-Safe Shared Resource Manager

MediumPython

Problem

CMT DriveWell services may share a bounded pool of reusable resources. Implement a thread-safe resource manager that allocates the smallest available resource identifier and safely returns resources to the pool.

The manager must support non-blocking acquisition. If no resource is available, acquisition returns -1. Releasing an unknown, already released, or out-of-range identifier returns False and must not change the pool.

Formal Specification

Implement process_resource_operations(operations, capacity):

  • operations is a list of commands. An acquire command is ["acquire"]; a release command is ["release", resource_id].
  • capacity is the number of resources, identified by integers from 0 through capacity - 1.
  • Return one result for every command. An acquire result is the allocated integer ID or -1. A release result is True or False.
  • The underlying manager must use synchronization so concurrent calls cannot allocate the same resource or corrupt its state.

Constraints

  • 1 <= capacity <= 10^5
  • 0 <= operations.length <= 2 * 10^5
  • Each operation is either ["acquire"] or ["release", resource_id]
  • resource_id may be outside the valid range
  • The manager may be accessed concurrently by multiple threads

Function Signature

def process_resource_operations(operations, capacity):
Interviewer

Your question is Thread-Safe Shared Resource Manager. 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.