Job Scheduling with Deadlines in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to schedule jobs grows as the number of jobs increases.
How does the code handle more jobs and deadlines efficiently?
Analyze the time complexity of the following code snippet.
function jobScheduling(jobs: {id: number, deadline: number, profit: number}[]): number[] {
jobs.sort((a, b) => b.profit - a.profit);
const result: number[] = [];
const slot: boolean[] = Array(jobs.length).fill(false);
for (const job of jobs) {
for (let j = Math.min(jobs.length, job.deadline) - 1; j >= 0; j--) {
if (!slot[j]) {
slot[j] = true;
result.push(job.id);
break;
}
}
}
return result;
}
This code schedules jobs to maximize profit by placing each job in the latest available slot before its deadline.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where the outer loop goes through each job and the inner loop searches for a free slot before the job's deadline.
- How many times: Outer loop runs once per job (n times), inner loop can run up to n times in worst case.
As the number of jobs grows, the code checks more jobs and searches more slots for each job.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 100 checks (10 jobs x 10 slots) |
| 100 | Up to 10,000 checks (100 jobs x 100 slots) |
| 1000 | Up to 1,000,000 checks (1000 jobs x 1000 slots) |
Pattern observation: The number of operations grows roughly with the square of the number of jobs.
Time Complexity: O(n2)
This means if you double the number of jobs, the time to schedule them roughly quadruples.
[X] Wrong: "The inner loop only runs once per job, so the code is O(n)."
[OK] Correct: Sometimes the inner loop must check many slots before finding a free one, especially when deadlines overlap, causing many repeated checks.
Understanding how nested loops affect time helps you explain your approach clearly and shows you can analyze efficiency in real problems.
"What if we used a data structure to find free slots faster? How would the time complexity change?"
Practice
Job Scheduling with Deadlines problem?Solution
Step 1: Understand the problem goal
The problem asks to schedule jobs so that total profit is maximized while respecting deadlines.Step 2: Identify the objective
Scheduling jobs before their deadlines to get maximum profit is the main goal.Final Answer:
Maximize the total profit by scheduling jobs before their deadlines -> Option DQuick Check:
Goal = Maximize profit with deadlines [OK]
- Ignoring deadlines and scheduling all jobs
- Trying to minimize jobs instead of maximizing profit
- Scheduling jobs in input order without sorting
id, deadline, and profit properties?Solution
Step 1: Check correct TypeScript interface syntax
interface Job { id: number; deadline: number; profit: number; } correctly declares an interface with typed properties.Step 2: Identify incorrect options
type Job = { id: string; deadline: string; profit: string } uses strings instead of numbers, class Job { id; deadline; profit; } is a class without types, interface Job { id; deadline; profit; } misses types.Final Answer:
interface Job { id: number; deadline: number; profit: number; } -> Option CQuick Check:
Correct interface syntax = interface Job { id: number; deadline: number; profit: number; } [OK]
- Using class without types for data structure
- Omitting types in interface properties
- Using wrong types like string for numeric fields
interface Job {
id: string;
deadline: number;
profit: number;
}
const jobs: Job[] = [
{ id: 'a', deadline: 2, profit: 100 },
{ id: 'b', deadline: 1, profit: 19 },
{ id: 'c', deadline: 2, profit: 27 },
{ id: 'd', deadline: 1, profit: 25 },
{ id: 'e', deadline: 3, profit: 15 }
];
function scheduleJobs(jobs: Job[]): string[] {
jobs.sort((a, b) => b.profit - a.profit);
const result: (string | null)[] = Array(jobs.length).fill(null);
for (const job of jobs) {
for (let j = Math.min(jobs.length, job.deadline) - 1; j >= 0; j--) {
if (result[j] === null) {
result[j] = job.id;
break;
}
}
}
return result.filter(id => id !== null) as string[];
}
console.log(scheduleJobs(jobs));Solution
Step 1: Sort jobs by profit descending
Sorted jobs: a(100), c(27), d(25), b(19), e(15).Step 2: Schedule jobs in latest available slot before deadline
Job 'a' scheduled at slot 1 (deadline 2), 'c' at slot 0 (deadline 2), 'd' cannot fit before deadline 1 (slot 0 taken), 'b' cannot fit, 'e' scheduled at slot 2 (deadline 3).Final Answer:
['a', 'c', 'e'] -> Option BQuick Check:
Scheduled jobs = ['a', 'c', 'e'] [OK]
- Scheduling jobs in input order without sorting
- Assigning earliest slot instead of latest
- Ignoring deadline constraints
function scheduleJobs(jobs: Job[]): string[] {
jobs.sort((a, b) => a.profit - b.profit);
const result: (string | null)[] = Array(jobs.length).fill(null);
for (const job of jobs) {
for (let j = job.deadline - 1; j >= 0; j--) {
if (result[j] === null) {
result[j] = job.id;
break;
}
}
}
return result.filter(id => id !== null) as string[];
}Solution
Step 1: Analyze sorting order
Jobs are sorted by profit ascending, which schedules low profit jobs first, reducing total profit.Step 2: Correct sorting order
Sorting by profit descending ensures high profit jobs are scheduled first for maximum profit.Final Answer:
Sort jobs by profit descending instead of ascending -> Option AQuick Check:
Profit descending sort fixes scheduling [OK]
- Sorting by ascending profit reduces total profit
- Changing loop direction incorrectly
- Using zero instead of null for empty slots
Solution
Step 1: Sort jobs by profit descending
Order: Job4(100), Job2(80), Job1(70), Job5(40), Job3(30).Step 2: Schedule jobs in latest free slot before deadline
Job4 at slot 1 (deadline 2), Job2 at slot 0 (deadline 1), Job1 at slot 3 (deadline 4), Job5 at slot 2 (deadline 3), Job3 cannot fit (slot 0 taken).Step 3: Calculate total profit
100 + 80 + 70 + 40 = 290, but slots are 0-based: slots 0,1,2,3 used. Actually, Job2 (deadline 1) at slot 0, Job4 (deadline 2) at slot 1, Job5 (deadline 3) at slot 2, Job1 (deadline 4) at slot 3. Total profit = 100 + 80 + 40 + 70 = 290.Step 4: Re-check options
Confirm scheduling: slots 0:Job2, 1:Job4, 2:Job5, 3:Job1. All fit, total 290.Final Answer:
290 -> Option AQuick Check:
Max profit = 290 [OK]
- Assuming all jobs fit without checking slots
- Adding profits of unscheduled jobs
- Misinterpreting deadlines as exact time slots
Ready to prove it?
Pass the test and this pattern moves unknown
