Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
A dynamic array doubles its capacity when full, copying all elements. Starting from capacity 1, what is the total number of element copies caused by resizing while inserting 1,000 elements, and what is the amortised cost per insertion?
- A. 999 copies; O(log n) amortised
- B. 500,500 copies; O(n) amortised
- C. 2,047 copies; O(1) amortised
- D. 1,023 copies; O(1) amortised
Correct answer: D. 1,023 copies; O(1) amortised
Resizes happen at sizes 1, 2, 4, …, 512, copying 1 + 2 + … + 512 = 1,023 elements in total, less than 2n, so the amortised cost per insertion is O(1).
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
- Evaluate the postfix expression 7 4 1 ÷ 7 + 6 × 2 × + 3 − (all divisions are exact). What is its value, and what is the maximum number of…
- Asteroids move along a line: positive values move right, negative move left; on collision the smaller (by absolute value) explodes, equal…
- One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i]…
- Keys 47, 74, 63, 43, 45, 59, 85 are inserted in this order into an empty table of size 13 using quadratic probing: probe i goes to (k mod…
- What does this loop-counting code print? (Python) python n, c = 300, 0 i = 2 while i <= n: c += 1 i = i i print(c)
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 1000, 0, 1000, 0…