0
0
DSA Cprogramming~10 mins

Job Scheduling with Deadlines in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the Job structure with id and deadline fields.

DSA C
typedef struct Job {
    int id;
    int [1];
} Job;
Drag options to blanks, or click blank then click option'
Apriority
Bduration
Cdeadline
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'priority' instead of 'deadline' for the deadline field.
Confusing 'duration' with deadline.
2fill in blank
medium

Complete the code to sort jobs by their deadline in ascending order.

DSA C
int compareJobs(const void *a, const void *b) {
    Job *jobA = (Job *)a;
    Job *jobB = (Job *)b;
    return jobA->[1] - jobB->[1];
}
Drag options to blanks, or click blank then click option'
Aduration
Bdeadline
Cpriority
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by id instead of deadline.
Using priority or duration which are not relevant here.
3fill in blank
hard

Fix the error in the code to schedule jobs within their deadlines using a greedy approach.

DSA C
int scheduleJobs(Job jobs[], int result[], int 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 - 1; j >= 0; j--) {
            if (slot[j] == [1]) {
                result[j] = i;
                slot[j] = 1;
                break;
            }
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if slot[j] == 1 which means occupied, so no assignment happens.
Using negative or invalid values for slot check.
4fill in blank
hard

Fill both blanks to print the scheduled jobs in order.

DSA C
void printSchedule(Job jobs[], int result[], int n) {
    printf("Scheduled jobs: ");
    for (int i = 0; i < n; i++) {
        if (result[i] != [1]) {
            printf("%d ", jobs[result[i]].[2]);
        }
    }
    printf("\n");
}
Drag options to blanks, or click blank then click option'
A-1
B0
Cid
Ddeadline
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 instead of -1 in result array.
Printing deadline instead of job id.
5fill in blank
hard

Fill all three blanks to initialize the result array with -1 and sort jobs by deadline.

DSA C
void initializeAndSort(Job jobs[], int n, int result[]) {
    for (int i = 0; i < n; i++) {
        result[i] = [1];
    }
    qsort(jobs, n, sizeof(Job), [2]);
}

int compareByDeadline(const void *a, const void *b) {
    Job *jobA = (Job *)a;
    Job *jobB = (Job *)b;
    return jobA->[3] - jobB->[3];
}
Drag options to blanks, or click blank then click option'
A-1
BcompareByDeadline
Cdeadline
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with 0 instead of -1.
Using wrong compare function name in qsort.
Comparing by id instead of deadline.