0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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 Algorithm
What is the output of the following C code that schedules jobs to maximize profit given deadlines?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char id;
    int deadline;
    int profit;
} Job;

int compare(const void *a, const void *b) {
    Job *j1 = (Job *)a;
    Job *j2 = (Job *)b;
    return j2->profit - j1->profit;
}

void printJobScheduling(Job jobs[], int n) {
    qsort(jobs, n, sizeof(Job), compare);

    int result[n];
    int slot[n];

    for (int i = 0; i < n; i++)
        slot[i] = 0;

    for (int i = 0; i < n; i++) {
        for (int j = (jobs[i].deadline < n ? jobs[i].deadline : n) - 1; j >= 0; j--) {
            if (slot[j] == 0) {
                result[j] = i;
                slot[j] = 1;
                break;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        if (slot[i])
            printf("%c ", jobs[result[i]].id);
    }
    printf("\n");
}

int main() {
    Job jobs[] = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}};
    int n = sizeof(jobs) / sizeof(jobs[0]);
    printJobScheduling(jobs, n);
    return 0;
}
Aa b c
Ba c e
Cb d e
Da d e
Attempts:
2 left
💡 Hint
Jobs are sorted by profit and scheduled in the latest available slot before their deadline.
🧠 Conceptual
intermediate
1:30remaining
Maximum Number of Jobs Scheduled
Given 5 jobs with deadlines and profits, what is the maximum number of jobs that can be scheduled without missing deadlines?
A2
B4
C5
D3
Attempts:
2 left
💡 Hint
Maximum jobs scheduled equals the number of available slots filled by the greedy algorithm.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Job Scheduling Code
What error will the following code produce when run?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char id;
    int deadline;
    int profit;
} Job;

int compare(const void *a, const void *b) {
    Job *j1 = (Job *)a;
    Job *j2 = (Job *)b;
    return j1->profit - j2->profit; // Bug here
}

void printJobScheduling(Job jobs[], int n) {
    qsort(jobs, n, sizeof(Job), compare);

    int result[n];
    int slot[n];

    for (int i = 0; i < n; i++)
        slot[i] = 0;

    for (int i = 0; i < n; i++) {
        for (int j = (jobs[i].deadline < n ? jobs[i].deadline : n) - 1; j >= 0; j--) {
            if (slot[j] == 0) {
                result[j] = i;
                slot[j] = 1;
                break;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        if (slot[i])
            printf("%c ", jobs[result[i]].id);
    }
    printf("\n");
}

int main() {
    Job jobs[] = {{'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27}, {'d', 1, 25}, {'e', 3, 15}};
    int n = sizeof(jobs) / sizeof(jobs[0]);
    printJobScheduling(jobs, n);
    return 0;
}
AThe jobs are sorted in ascending profit order, causing incorrect scheduling output.
BCompilation error due to missing semicolon.
CRuntime error due to accessing invalid array index.
DNo output because of infinite loop.
Attempts:
2 left
💡 Hint
Check the compare function used in qsort.
📝 Syntax
advanced
1:30remaining
Syntax Error in Job Scheduling Code
Which option contains a syntax error in the job scheduling code snippet?
DSA C
typedef struct {
    char id;
    int deadline;
    int profit;
} Job;

int compare(const void *a, const void *b) {
    Job *j1 = (Job *)a;
    Job *j2 = (Job *)b;
    return j2->profit - j1->profit;
}

void printJobScheduling(Job jobs[], int n) {
    qsort(jobs, n, sizeof(Job), compare);

    int result[n];
    int slot[n];

    for (int i = 0; i < n; i++)
        slot[i] = 0;

    for (int i = 0; i < n; i++) {
        for (int j = (jobs[i].deadline < n ? jobs[i].deadline : n) - 1; j >= 0; j--) {
            if (slot[j] == 0) {
                result[j] = i;
                slot[j] = 1;
                break;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        if (slot[i])
            printf("%c ", jobs[result[i]].id);
    }
    printf("\n");
}
AMissing closing brace for printJobScheduling function.
BIncorrect pointer casting in compare function.
CUsing variable length arrays without initialization.
DMissing semicolon after struct definition.
Attempts:
2 left
💡 Hint
Check the braces and function endings carefully.
🚀 Application
expert
2:30remaining
Optimal Job Scheduling Result for Custom Input
Given the following jobs with deadlines and profits, which sequence of job IDs maximizes profit without missing deadlines? Jobs: {'j1', deadline=1, profit=20} {'j2', deadline=2, profit=15} {'j3', deadline=2, profit=10} {'j4', deadline=1, profit=5} {'j5', deadline=3, profit=1} Choose the correct scheduled job sequence output.
Aj4 j3 j5
Bj1 j3 j5
Cj1 j2 j5
Dj2 j3 j5
Attempts:
2 left
💡 Hint
Sort jobs by profit descending and assign to latest free slot before deadline.