Data Structures & AlgorithmsModerateProduct Company SDE · complexity round
Problem: A dynamic array doubles its capacity whenever it is full and copies all elements. Which statement about n pushes is correct?
- A. Each push is worst-case O(1) and amortized O(n)
- B. Each push costs O(log n) always
- C. Each push is amortized O(1), but a single push can cost O(n) during a resize
- D. Each push costs O(n) always
Correct answer: C. Each push is amortized O(1), but a single push can cost O(n) during a resize
Copies happen at powers of two, so the total copying work over n pushes is below 2n, giving amortized O(1). A single resizing push still takes O(n) time.
More Coding Interview Problems questions
- Problem: How many ways are there to tile a 2 x 10 board completely with 2 x 1 dominoes (placed horizontally or vertically)?
- Array a = [1, -1, 2, 3, -2, 4, -3, 1]. How many contiguous non-empty subarrays have sum exactly 3? Solve it with running prefix sums and a…
- For a = [3, 4, -7, 2, -2, 5, -5, 1, 6], what is the length of the longest contiguous subarray whose elements sum to 0?
- Array a = [-2, 3, -4, 6, -1, 2]. How many non-empty subarrays have a sum divisible by 5? Count prefix sums by their remainder, normalised…
- In the binary string s = "1100110100", count the non-empty substrings that contain exactly as many 0s as 1s.
- Array a = [2, 7, -3, 5, 11, 4, 9, -1, 6]. How many index pairs (i, j) with i < j have a[i] + a[j] = 10?