Skip to content
Zeni Study
  • Pages
    • Learn Python In 7 Days
      • Day 1 : Variable + if/else
      • Day 2: Loops
      • Day 3 : Array + String
      • Day 4 — Functions
      • Day 5 — Dictionaries - Hashmap
      • Day 6 — Files & Errors
      • Day 7 — Mini Project & Programmer Thinking
    • Machine Coding:
      • Day 1 — Bank account + Wallet :
      • Day 2 : RateLimiter + seat booking
      • Day 3 : Library Management + Classroom Attendance System
      • Day 4 : Bank account and notification system.
      • Day 5 : Key-value and payment processor.
      • Day 6 : Discount engine + Seat booking( in depth)
      • Day 7: Parking lot System :
    • Machine coding extended:
      • Day 7.5 — Parking Lot (Constraint-Heavy Variant)
      • Day 8 : Order + Payment System
      • Day 8 — Extension Round (Mid-Interview Change)
      • Day 9 — Ticket Booking with Expiry (Simulated Time)
      • Day 9 — Extension Round (Seat Lock Ownership + Forced Unlock)
    • SQL + DB Thinking course
      • Day 1 — Tables, Rows & Thinking in Data
      • Day 2 — Relationships & Foreign Keys (Thinking in Connections)
      • Day 3 — Filtering, Aggregation & Answering Data Questions
      • Day 4 — JOINs (Combining Tables the Right Way)
      • Day 5 — Subqueries, EXISTS & NOT EXISTS (Thinking in Layers)
      • Day 6 — Indexes, Constraints & How Databases Think
      • Day 7 — Full SQL Interview Simulation (SDE-1 Ready)
      • Extended Day 1 — Core Business Systems (Deep Practice)
      • Extended Day 2 — Booking & Platform Systems (High-Depth Practice)
      • Extended Day 3 — Content & Learning Platforms (Capstone Practice)
    • LLD: Java
      • DAY 1 — Core Foundations (SDE-2 Level)
      • DAY 2 — State, Time & Domain Correctness
      • DAY 3 — Extensibility & Failure Handling (SDE-2 Core Signal)
      • icon picker
        DAY 4 — Concurrency, Scheduling & Consistency (SDE-2 Critical)
      • DAY 5 — Workflow Design & Config-Driven Systems (SDE-2+)
      • DAY 6 — Eventing & Usage-Based Systems (Advanced SDE-2)
      • DAY 7 — Interview Simulation (Hard, Production-Grade)
      • Hidden Edge Cases Interviewers Expect (Day-wise)
      • Detailed Testing file:
    • Assignment:
      • If else Practice Set

DAY 4 — Concurrency, Scheduling & Consistency (SDE-2 Critical)

Excellent. ​Day 4 is where many SDE-2 candidates break — because now concurrency + correctness are tested together.
Interviewers expect:
You know Java concurrency primitives
You protect invariants under race conditions
You don’t over-engineer, but you also don’t hand-wave
You’ll feel this day is harder than previous ones — that’s intentional.

🔵 DAY 4 — Concurrency, Scheduling & Consistency (SDE-2 Critical)

Concepts Introduced Today (Short + Interview-Relevant)

1️⃣ Concurrency ≠ Multithreading

Concurrency is about:
Correctness under race
Atomicity
Visibility
A solution that “works in single thread” is ❌ today.

2️⃣ Time + Threads = Tricky

Schedulers, delays, cancellation — interviewers check:
Do tasks execute at the right time?
Do canceled tasks really stop?
Does the system leak threads?

3️⃣ Consistency Over Performance

Overselling inventory or executing a task twice is instant rejection.

✅ Problem 1: Concurrent Task Scheduler

📌 Problem Statement

Design an in-memory task scheduler that:
Executes tasks at a future timestamp
Supports cancellation
Is thread-safe

Functional Requirements

schedule(taskId, runnable, executeAtMillis)
cancel(taskId)
Task should run once
Cancelled tasks must never execute

Example

schedule("t1", task, now + 2 sec)
cancel("t1")
→ task never runs

🧩 Java Interfaces (DO NOT MODIFY)

public interface TaskScheduler {
void schedule(String taskId, Runnable task, long executeAtMillis);
boolean cancel(String taskId);
}

🧪 Driver Code (Auto Tests + Edge Cases)

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class TaskSchedulerTest {

public static void main(String[] args) throws Exception {
TaskScheduler scheduler = new InMemoryTaskScheduler();

AtomicInteger counter = new AtomicInteger(0);

scheduler.schedule("task1", counter::incrementAndGet,
System.currentTimeMillis() + 1000);

Thread.sleep(1500);
assert counter.get() == 1;

// Cancellation test
scheduler.schedule("task2", counter::incrementAndGet,
System.currentTimeMillis() + 1000);
boolean cancelled = scheduler.cancel("task2");
assert cancelled;

Thread.sleep(1500);
assert counter.get() == 1;

// Cancel non-existent
assert !scheduler.cancel("missing");

System.out.println("✅ TaskScheduler tests passed");
}
}

🎯 What Interviewer Evaluates

Correct scheduling
Thread safety
No double execution
Clean shutdown strategy (if asked)

✅ Problem 2: Inventory Management System (Oversell Prevention)

📌 Problem Statement

Design an inventory system that supports:
Adding stock
Reserving stock
Releasing stock
Preventing oversell under concurrency

Functional Requirements

 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.