0
0
DSA Cprogramming~20 mins

Why Greedy Works and When It Fails in DSA C - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Greedy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Greedy Coin Change Algorithm
What is the output of the following C code that uses a greedy approach to make change for 30 cents with coin denominations 25, 10, and 1?
DSA C
#include <stdio.h>

int main() {
    int coins[] = {25, 10, 1};
    int amount = 30;
    int count[3] = {0};

    for (int i = 0; i < 3; i++) {
        while (amount >= coins[i]) {
            amount -= coins[i];
            count[i]++;
        }
    }

    printf("Coins used: 25c=%d, 10c=%d, 1c=%d\n", count[0], count[1], count[2]);
    return 0;
}
ACoins used: 25c=1, 10c=0, 1c=0
BCoins used: 25c=0, 10c=3, 1c=0
CCoins used: 25c=1, 10c=0, 1c=5
DCoins used: 25c=0, 10c=2, 1c=10
Attempts:
2 left
💡 Hint
Think about how the greedy algorithm picks the largest coin first and then moves to smaller coins.
🧠 Conceptual
intermediate
2:00remaining
When Does Greedy Fail in Coin Change?
Why does the greedy algorithm fail to find the minimum number of coins for the amount 6 cents when the coin denominations are 4, 3, and 1?
ABecause the greedy algorithm picks the largest coin first, it chooses 4, then two 1-cent coins, totaling 3 coins, but the optimal is two 3-cent coins, which is 2 coins total.
BBecause the greedy algorithm picks the largest coin first, it chooses 4, then two 1-cent coins, totaling 3 coins, but the optimal is one 3-cent coin and three 1-cent coins, which is 4 coins total.
CBecause the greedy algorithm picks the largest coin first, it chooses 3, then 3, totaling 2 coins, but the optimal is something else.
DThe greedy algorithm actually finds the optimal solution here.
Attempts:
2 left
💡 Hint
Check how many coins the greedy picks versus the optimal solution with these denominations.
Predict Output
advanced
2:00remaining
Output of Interval Scheduling Greedy Algorithm
What is the output of the following C code that selects the maximum number of non-overlapping intervals using a greedy approach?
DSA C
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int start;
    int end;
} Interval;

int compare(const void *a, const void *b) {
    Interval *i1 = (Interval *)a;
    Interval *i2 = (Interval *)b;
    return i1->end - i2->end;
}

int main() {
    Interval intervals[] = {{1,4}, {3,5}, {0,6}, {5,7}, {8,9}, {5,9}};
    int n = sizeof(intervals)/sizeof(intervals[0]);

    qsort(intervals, n, sizeof(Interval), compare);

    int count = 1;
    int last_end = intervals[0].end;

    for (int i = 1; i < n; i++) {
        if (intervals[i].start >= last_end) {
            count++;
            last_end = intervals[i].end;
        }
    }

    printf("Maximum non-overlapping intervals: %d\n", count);
    return 0;
}
AMaximum non-overlapping intervals: 5
BMaximum non-overlapping intervals: 3
CMaximum non-overlapping intervals: 4
DMaximum non-overlapping intervals: 2
Attempts:
2 left
💡 Hint
The greedy algorithm sorts intervals by end time and picks intervals that start after the last chosen interval ends.
🔧 Debug
advanced
2:00remaining
Identify the Error in Greedy Activity Selector
What error does the following C code produce when trying to select maximum activities using a greedy approach?
DSA C
#include <stdio.h>

int main() {
    int start[] = {1, 3, 0, 4, 8, 5};
    int finish[] = {2, 4, 6, 7, 9, 9};
    int n = 6;
    int count = 1;
    int last_finish = finish[0];

    for (int i = 1; i < n; i++) {
        if (start[i] > last_finish) {
            count++;
            last_finish = finish[i];
        }
    }

    printf("Number of activities selected: %d\n", count);
    return 0;
}
AThe code outputs: Number of activities selected: 1
BThe code outputs: Number of activities selected: 4
CThe code outputs: Number of activities selected: 2
DThe code outputs: Number of activities selected: 3
Attempts:
2 left
💡 Hint
Check the condition for selecting activities and the sorting of activities by finish time.
🚀 Application
expert
3:00remaining
Greedy Algorithm Failure in Job Scheduling with Deadlines
Given jobs with deadlines and profits, which greedy approach fails to maximize profit? Jobs: {Job1: deadline=2, profit=100}, {Job2: deadline=1, profit=19}, {Job3: deadline=2, profit=27}, {Job4: deadline=1, profit=25}, {Job5: deadline=3, profit=15}. Which option shows the incorrect greedy approach and its resulting profit?
AScheduling jobs in ascending order of deadline and assigning latest available slot results in total profit 134.
BScheduling jobs in ascending order of profit and assigning earliest available slot results in total profit 61.
CScheduling jobs in descending order of profit and assigning latest available slot results in total profit 142.
DScheduling jobs in descending order of profit and assigning earliest available slot results in total profit 142.
Attempts:
2 left
💡 Hint
The correct greedy approach schedules jobs by descending profit and assigns the latest available slot before deadline.