ProgrammingHard
A dynamic array doubles its capacity whenever it is full. Pushing N = 10^6 values costs about how many element copies in total?
- A. About 10^12, so the doubling is quadratic
- B. About 10^6 log 10^6, which is about 2 x 10^7
- C. Under 2 x 10^6 in total, so the amortised cost per push is O(1)
- D. About 500 000 because only half are copied
Correct answer: C. Under 2 x 10^6 in total, so the amortised cost per push is O(1)
The copies form a geometric series: 1 + 2 + 4 + ... < 2N. Each element is copied only a constant number of times on average, so each push is O(1) amortised.
More C Programming questions
- A function returns a pointer to a node created with malloc inside it. Which description is correct?
- Which statement about calloc(n, size) is guaranteed by the C standard?
- A program calls free(p) and then reads p->v in the next line. What is the correct description?
- A hash chain delete on a singly linked bucket list is written with a pointer-to-pointer. Which description is the advantage?
- Building a list from n values by inserting each new node at the head is O(1) per insert. What is the order of the values when read from…
- Which C statement is correct for a node with an int field v and a next pointer, allocated and initialised to v = 7 with no next node?