Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: You want the maximum number of non-overlapping intervals. Which greedy rule is provably optimal?
- A. Earliest finish time first
- B. Earliest start time first
- C. Shortest interval first
- D. Latest start time first
Correct answer: A. Earliest finish time first
Picking the interval that finishes earliest leaves the most room for the rest (exchange argument). Earliest start fails on [1,10], [2,3], [4,5], where it picks only one interval but the optimum is two.
More Coding Interview Problems questions
- Problem: Count subarrays whose XOR is zero, for a = [1, 2, 3, 0, 3, 3]. python from collections import Counter def cnt(a): seen =…
- Problem: What does the following print? python print([format(i ^ (i >> 1), '03b') for i in range(4)])
- Problem: Count the pairs (i < j) with a[i] + a[j] < 6 for the array [5, 1, 4, 2, 3]. python def count(a, T): a = sorted(a) i, j = 0…
- Problem: Mo's algorithm answers q range queries on an array of length n by sorting queries into blocks of size about sqrt(n) and moving…
- Problem: In a segment tree with lazy propagation, what is the cost of one range-add update on an array of length n?
- Problem: What does the following print? python print(-7 // 2, -7 % 2, 7 // -2, int(-7 / 2))