ProgrammingHardAmazon OA · Flipkart coding round
Which of the following problems can be solved correctly by a greedy algorithm that sorts by end time, when given the intervals and asked for the maximum number of non-overlapping intervals?
- A. Activity selection: sort by finish time and keep each interval that starts after the last one kept
- B. Minimum number of coins for an arbitrary coin set, sorting coins by value
- C. 0/1 knapsack, sorting items by value per weight
- D. Longest path in a general weighted graph, sorting edges by weight
Correct answer: A. Activity selection: sort by finish time and keep each interval that starts after the last one kept
Interval scheduling is solved by earliest finish time: choosing the interval that ends first leaves the most room. The other problems need DP or are not solved by this greedy rule.
More Python Programming questions
- What is printed? python a = [1, 2, 3, 4, 5, 6] print(a[1::2], a[::-2], a[4:1:-1])
- What is printed? python s = 'a1b22c333' import re print(re.findall(r'd+', s), sum(int(x) for x in re.findall(r'd+', s)))
- What is printed? python def solve(n, memo={}): if n in memo: return memo[n] if n < 2: return n memo[n] = solve(n - 1) + solve(n - 2)…
- What is printed? python x = 7 match x: case 1 | 2: r = 'small' case int(v) if v > 5: r = f'big{v}' case : r = 'other' print(r)
- What is printed? python a = [[1, 2], [3]] b = a[:] b[1].append(4) b.append([5]) print(a, b, len(a))
- What is printed? python print(bool([]), bool([0]), bool(0.0), bool('False'), bool(None) or 'x')