Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Job Scheduling with Deadlines in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Job Scheduling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Job Scheduling with Deadlines and Profits
What is the output of the following TypeScript code that schedules jobs to maximize profit given deadlines?
DSA Typescript
interface Job {
  id: string;
  deadline: number;
  profit: number;
}

function jobScheduling(jobs: Job[]): string {
  jobs.sort((a, b) => b.profit - a.profit);
  const n = jobs.length;
  const result: (string | null)[] = Array(n).fill(null);
  const slot: boolean[] = Array(n).fill(false);

  for (let i = 0; i < n; i++) {
    for (let j = Math.min(n - 1, jobs[i].deadline - 1); j >= 0; j--) {
      if (!slot[j]) {
        slot[j] = true;
        result[j] = jobs[i].id;
        break;
      }
    }
  }

  return result.filter(x => x !== null).join(' -> ') + ' -> null';
}

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 }
];

console.log(jobScheduling(jobs));
A"a -> c -> e -> null"
B"a -> d -> e -> null"
C"b -> d -> e -> null"
D"a -> b -> c -> null"
Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign each job to the latest available slot before its deadline.
🧠 Conceptual
intermediate
1:00remaining
Maximum Number of Jobs Scheduled
Given 5 jobs with deadlines [2, 1, 2, 1, 3] and profits [100, 19, 27, 25, 15], what is the maximum number of jobs that can be scheduled without missing deadlines?
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Each job takes 1 unit of time. Schedule jobs greedily by profit and assign to latest free slot before deadline.
🔧 Debug
advanced
1:30remaining
Identify the Logical Error in Job Scheduling Code
What is the logical error in the following TypeScript code snippet for job scheduling with deadlines? function jobScheduling(jobs: Job[]): string { jobs.sort((a, b) => a.profit - b.profit); const n = jobs.length; const result: (string | null)[] = Array(n).fill(null); const slot: boolean[] = Array(n).fill(false); for (let i = 0; i < n; i++) { for (let j = Math.min(n - 1, jobs[i].deadline - 1); j >= 0; j--) { if (!slot[j]) { slot[j] = true; result[j] = jobs[i].id; break; } } } return result.filter(x => x !== null).join(' -> ') + ' -> null'; }
ASlots array is not initialized correctly, causing index out of range errors.
BThe result array should store profits instead of job ids for correct output.
CThe inner loop should iterate forwards, not backwards, to assign jobs correctly.
DJobs are sorted in ascending order of profit instead of descending, causing suboptimal scheduling.
Attempts:
2 left
💡 Hint
Check the sorting order of jobs by profit.
📝 Syntax
advanced
1:30remaining
Identify Syntax Error in Job Scheduling Code
Which option contains a syntax error in the TypeScript code for job scheduling?
DSA Typescript
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 }
];

function scheduleJobs(jobs: Job[]): string {
  jobs.sort((a, b) => b.profit - a.profit);
  const n = jobs.length;
  const result: (string | null)[] = Array(n).fill(null);
  const slot: boolean[] = Array(n).fill(false);

  for (let i = 0; i < n; i++) {
    for (let j = Math.min(n - 1, jobs[i].deadline - 1); j >= 0; j--) {
      if (!slot[j]) {
        slot[j] = true;
        result[j] = jobs[i].id;
        break;
      }
    }
  }

  return result.filter(x => x !== null).join(' -> ') + ' -> null';
}
AMissing semicolon after slot[j] = true inside the inner loop.
BIncorrect type annotation for result array; should be string[] not (string | null)[]
CUsing Math.min with n - 1 and jobs[i].deadline - 1 causes runtime error.
DThe function scheduleJobs is missing a return type annotation.
Attempts:
2 left
💡 Hint
Look carefully at statement endings inside loops.
🚀 Application
expert
2:30remaining
Optimal Job Scheduling Output for Complex Input
Given the following jobs with deadlines and profits, what is the optimal job schedule output string? Jobs: - {id: 'j1', deadline: 4, profit: 70} - {id: 'j2', deadline: 1, profit: 80} - {id: 'j3', deadline: 1, profit: 30} - {id: 'j4', deadline: 2, profit: 100} - {id: 'j5', deadline: 3, profit: 40} - {id: 'j6', deadline: 2, profit: 20} Use the greedy algorithm to schedule jobs to maximize profit. Each job takes 1 unit time. Output format: job ids separated by ' -> ' ending with ' -> null'.
A"j2 -> j4 -> j5 -> j1 -> null"
B"j4 -> j2 -> j5 -> j1 -> null"
C"j4 -> j5 -> j1 -> j2 -> null"
D"j2 -> j4 -> j1 -> j5 -> null"
Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign each to the latest free slot before deadline.

Practice

(1/5)
1. What is the main goal of the Job Scheduling with Deadlines problem?
easy
A. Maximize the number of jobs without considering profit
B. Minimize the number of jobs scheduled
C. Schedule jobs in the order they appear without considering deadlines
D. Maximize the total profit by scheduling jobs before their deadlines

Solution

  1. Step 1: Understand the problem goal

    The problem asks to schedule jobs so that total profit is maximized while respecting deadlines.
  2. Step 2: Identify the objective

    Scheduling jobs before their deadlines to get maximum profit is the main goal.
  3. Final Answer:

    Maximize the total profit by scheduling jobs before their deadlines -> Option D
  4. Quick Check:

    Goal = Maximize profit with deadlines [OK]
Hint: Focus on maximizing profit within deadlines [OK]
Common Mistakes:
  • Ignoring deadlines and scheduling all jobs
  • Trying to minimize jobs instead of maximizing profit
  • Scheduling jobs in input order without sorting
2. Which of the following TypeScript code snippets correctly declares a job interface with id, deadline, and profit properties?
easy
A. type Job = { id: string; deadline: string; profit: string }
B. class Job { id; deadline; profit; }
C. interface Job { id: number; deadline: number; profit: number; }
D. interface Job { id; deadline; profit; }

Solution

  1. Step 1: Check correct TypeScript interface syntax

    interface Job { id: number; deadline: number; profit: number; } correctly declares an interface with typed properties.
  2. 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.
  3. Final Answer:

    interface Job { id: number; deadline: number; profit: number; } -> Option C
  4. Quick Check:

    Correct interface syntax = interface Job { id: number; deadline: number; profit: number; } [OK]
Hint: Use interface with typed properties for job data [OK]
Common Mistakes:
  • Using class without types for data structure
  • Omitting types in interface properties
  • Using wrong types like string for numeric fields
3. What will be the output of the following TypeScript code that schedules jobs to maximize profit?
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));
medium
A. ['a', 'd', 'e']
B. ['a', 'c', 'e']
C. ['b', 'd', 'e']
D. ['a', 'b', 'c']

Solution

  1. Step 1: Sort jobs by profit descending

    Sorted jobs: a(100), c(27), d(25), b(19), e(15).
  2. 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).
  3. Final Answer:

    ['a', 'c', 'e'] -> Option B
  4. Quick Check:

    Scheduled jobs = ['a', 'c', 'e'] [OK]
Hint: Sort by profit, assign latest free slot before deadline [OK]
Common Mistakes:
  • Scheduling jobs in input order without sorting
  • Assigning earliest slot instead of latest
  • Ignoring deadline constraints
4. Identify the bug in the following TypeScript function for job scheduling and select the correct fix.
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[];
}
medium
A. Sort jobs by profit descending instead of ascending
B. Initialize result array with zeros instead of null
C. Loop j from 0 to job.deadline - 1 instead of reverse
D. Remove the inner loop and assign jobs directly

Solution

  1. Step 1: Analyze sorting order

    Jobs are sorted by profit ascending, which schedules low profit jobs first, reducing total profit.
  2. Step 2: Correct sorting order

    Sorting by profit descending ensures high profit jobs are scheduled first for maximum profit.
  3. Final Answer:

    Sort jobs by profit descending instead of ascending -> Option A
  4. Quick Check:

    Profit descending sort fixes scheduling [OK]
Hint: Sort jobs by descending profit to fix scheduling [OK]
Common Mistakes:
  • Sorting by ascending profit reduces total profit
  • Changing loop direction incorrectly
  • Using zero instead of null for empty slots
5. Given the following jobs with deadlines and profits, what is the maximum total profit achievable by scheduling jobs before their deadlines? Jobs: - Job1: deadline = 4, profit = 70 - Job2: deadline = 1, profit = 80 - Job3: deadline = 1, profit = 30 - Job4: deadline = 2, profit = 100 - Job5: deadline = 3, profit = 40 Select the correct maximum profit.
hard
A. 290
B. 210
C. 240
D. 230

Solution

  1. Step 1: Sort jobs by profit descending

    Order: Job4(100), Job2(80), Job1(70), Job5(40), Job3(30).
  2. 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).
  3. 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.
  4. Step 4: Re-check options

    Confirm scheduling: slots 0:Job2, 1:Job4, 2:Job5, 3:Job1. All fit, total 290.
  5. Final Answer:

    290 -> Option A
  6. Quick Check:

    Max profit = 290 [OK]
Hint: Schedule highest profit jobs first in latest free slot [OK]
Common Mistakes:
  • Assuming all jobs fit without checking slots
  • Adding profits of unscheduled jobs
  • Misinterpreting deadlines as exact time slots
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard