Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA
What is the maximum number of pairwise non-overlapping intervals (an interval [s, f) may touch the next one at f = s) chosen from [(1,4),(3,5),(0,6),(5,7),(3,9),(5,9),(6,10),(8,11),(8,12),(2,14),(12,16)]?
- A. 5
- B. 3
- C. 4
- D. 2
Correct answer: C. 4
Greedy by earliest finish time is optimal by an exchange argument: replacing the first chosen interval by the one that finishes earliest never hurts. Sorting by start time instead is a known failure.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A railway station receives trains at times 900, 940, 950, 1100, 1500, 1800 and each train leaves at 910, 1200, 1120, 1130, 1900, 2000…
- For the array [3, 1, 4, 1, 5, 9, 2, 6], how many subarrays have a product that is a multiple of 6?
- Let A = [5, 3, 6, 2, 7, 1] (0-based), with P[0] = 0 and P[i] = A[0] ⊕ … ⊕ A[i−1]. What is A[2] ⊕ A[3] ⊕ A[4]?
- How many non-empty contiguous subarrays of [1, 2, 3, 0, 3] have XOR equal to 0?
- What is the maximum value of A[i] ⊕ A[j] over pairs i < j in A = [3, 10, 5, 25, 2, 8]?
- How many unordered pairs {i, j} with i ≠ j in [1, 3, 5, 7, 2, 4] satisfy A[i] ⊕ A[j] = 6?