Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Deadlock Implementation

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Deadlock Implementation. Start with the requirements on the right.

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.

Problem

HPE Ezmeral workloads may have processes holding resource instances while waiting for additional instances. Given a snapshot of resource availability, allocations, and outstanding requests, return every process that can never complete because of a deadlock.

Use the standard deadlock-detection algorithm: assume a process can finish when all of its outstanding requests can be satisfied by the currently available resources. When it finishes, release its allocated resources and continue checking other processes. Processes that remain unfinished are deadlocked.

Formal Specification

Implement detect_deadlock(available, allocation, requests).

  • available is an integer array of length R, where available[j] is the number of currently free instances of resource type j.
  • allocation is an integer matrix with P rows and R columns. allocation[i][j] is held by process i.
  • requests is an integer matrix with the same dimensions. requests[i][j] is the additional number of instances process i needs to finish.
  • Return a sorted integer array containing the indices of all deadlocked processes. Return [] if no deadlock exists.

Constraints

  • 1 <= P, R <= 500
  • available has length R
  • allocation and requests are P x R matrices
  • All resource counts are non-negative integers
  • The returned process indices must be sorted

Function Signature

def detect_deadlock(available, allocation, requests):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output