PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Microsoft

Implement a type-based mutex for tasks

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of concurrency and synchronization primitives, testing the ability to design a thread-safe type-based mutex that permits concurrent execution of tasks with the same type while preventing concurrent execution across different types.

  • nan
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Implement a type-based mutex for tasks

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: nan

Interview Round: Technical Screen

## Problem You are given a multithreaded task runner. Each task has: - `task_id: int` - `task_type: "gen" | "score"` - `duration: int` (seconds) Each task runs in its own thread and calls a shared lock before and after doing work. ```python from dataclasses import dataclass from typing import Literal @dataclass class SampleTask: task_id: int task_type: Literal["gen", "score"] duration: int class TaskLock: def acquire(self, task: SampleTask) -> None: raise NotImplementedError def release(self, task: SampleTask) -> None: raise NotImplementedError ``` The task runner does: 1. `lock.acquire(task)` 2. sleeps for `task.duration` 3. `lock.release(task)` ## Task Implement a `MutexTaskLock(TaskLock)` such that: - Multiple tasks of the **same** `task_type` may run **concurrently**. - Tasks of **different** `task_type` must **not** run at the same time (i.e., if any `gen` task is running, no `score` task may run, and vice versa). - The implementation must be thread-safe and avoid deadlocks. You may use Python threading primitives such as `threading.Lock`, `threading.Condition`, or semaphores. ## Clarifications / Expected behavior - If no tasks are running, either type may acquire the lock. - If tasks of one type are running, tasks of the other type must block until all running tasks of the current type finish and release. - It is acceptable for waiting tasks to wake up and re-check conditions (i.e., use a loop around `wait`).

Quick Answer: This question evaluates understanding of concurrency and synchronization primitives, testing the ability to design a thread-safe type-based mutex that permits concurrent execution of tasks with the same type while preventing concurrent execution across different types.

Related Interview Questions

  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Implement SFT Sample Packing - Microsoft (medium)
  • Implement SQL Table and DNA Ordering - Microsoft (medium)
  • Solve power jumps and graph tour - Microsoft (hard)
Microsoft logo
Microsoft
Mar 1, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
8
0
Loading...

Problem

You are given a multithreaded task runner. Each task has:

  • task_id: int
  • task_type: "gen" | "score"
  • duration: int (seconds)

Each task runs in its own thread and calls a shared lock before and after doing work.

from dataclasses import dataclass
from typing import Literal

@dataclass
class SampleTask:
    task_id: int
    task_type: Literal["gen", "score"]
    duration: int

class TaskLock:
    def acquire(self, task: SampleTask) -> None:
        raise NotImplementedError

    def release(self, task: SampleTask) -> None:
        raise NotImplementedError

The task runner does:

  1. lock.acquire(task)
  2. sleeps for task.duration
  3. lock.release(task)

Task

Implement a MutexTaskLock(TaskLock) such that:

  • Multiple tasks of the same task_type may run concurrently .
  • Tasks of different task_type must not run at the same time (i.e., if any gen task is running, no score task may run, and vice versa).
  • The implementation must be thread-safe and avoid deadlocks.

You may use Python threading primitives such as threading.Lock, threading.Condition, or semaphores.

Clarifications / Expected behavior

  • If no tasks are running, either type may acquire the lock.
  • If tasks of one type are running, tasks of the other type must block until all running tasks of the current type finish and release.
  • It is acceptable for waiting tasks to wake up and re-check conditions (i.e., use a loop around wait ).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.