Data Structures & AlgorithmsHardTCS NQT · Product Company SDE
Problem: The dynamic array doubles its capacity whenever it is full, starting from capacity 1. After inserting 9 elements one by one, how many element copies occur in total during resizes (copying the existing elements at each resize)?
- A. 7
- B. 31
- C. 15
- D. 8
Correct answer: C. 15
Resizes occur when the array is full at sizes 1, 2, 4, 8, copying 1 + 2 + 4 + 8 = 15 elements before the 9th insert fits. Missing the resize at 8 gives 7, one extra doubling gives 31, and 8 counts only the last copy.
More Coding Interview Problems questions
- Problem: In a binary heap stored in an array with 1-based indexing, the array [1, 3, 2, 7, 4, 5, 6] is a valid min-heap. What is the sum…
- Problem: Vertices 0–5 form a graph with edges 0-1, 1-2, 2-3, 3-0 and 4-5. A BFS from vertex 0 gives each vertex a distance. What is the…
- Problem: A stack-sortable check is applied to the permutation [3, 1, 2]: elements are pushed in order and popped whenever the top equals…
- Problem: The array [5, 2, 8, 2, 9, 1, 5, 5] contains repeated values. How many distinct values appear exactly twice?
- Problem: What is the maximum sum of a subsequence of [4, −1, 2, 1] with no two adjacent elements chosen (the empty selection is not…
- Problem: The string "ABCABCABC" is read and each letter is replaced by its position in the alphabet (A=1, B=2, C=3). What is the sum of…