0
0
DSA Typescriptprogramming~10 mins

Why Greedy Works and When It Fails in DSA Typescript - Test Your Knowledge

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

Complete the code to select the next interval with the earliest finish time.

DSA Typescript
intervals.sort((a, b) => a.end [1] b.end);
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction in the sort comparator.
Sorting by start time instead of end time.
2fill in blank
medium

Complete the code to check if the current interval starts after the last selected interval ends.

DSA Typescript
if (interval.start [1] lastSelectedEnd) { selected.push(interval); lastSelectedEnd = interval.end; }
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= instead of >, which allows overlapping intervals.
Using < or <=, which is incorrect for this condition.
3fill in blank
hard

Fix the error in the greedy coin change algorithm that fails for non-standard coin sets.

DSA Typescript
function greedyChange(amount: number, coins: number[]): number[] {
  coins.sort((a, b) => b - a);
  const result: number[] = [];
  for (const coin of coins) {
    while (amount [1] coin) {
      amount -= coin;
      result.push(coin);
    }
  }
  return result;
}
Drag options to blanks, or click blank then click option'
A>=
B<=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of >= causes missing exact matches.
Using < or <= causes infinite loops or no coins used.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters words longer than 3 characters and maps to their lengths.

DSA Typescript
const lengths = { [1]: [2] for (const word of words) if (word.length > 3) };
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Cword.toUpperCase()
Dword.length > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the condition as a key or value.
Using uppercase words as keys instead of original words.
5fill in blank
hard

Fill all three blanks to create a map filtering entries with positive values and mapping keys to uppercase.

DSA Typescript
const filteredMap = new Map(Object.entries(data)
  .filter(([[1], [2]]) => [2] > 0)
  .map(([[1], [2]]) => [[3], [2]])
);
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Ckey.toUpperCase()
Dvalue.toUpperCase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using value.toUpperCase() which is invalid if value is a number.
Mixing up key and value in filter or map.