GATE Computer ScienceVery hardGATE CS
A dynamic array starts with capacity 1. Whenever an append finds it full, a new array of double the capacity is allocated and all current elements are copied. After 1000 appends, how many element copies have occurred in total because of resizing?
- A. 1023
- B. 1000
- C. 2047
- D. 1536
Correct answer: A. 1023
Resizing occurs when appending elements 2, 3, 5, 9, ..., 513, copying 1, 2, 4, ..., 512 elements. The total is 2^10 − 1 = 1023, which is why the amortised cost per append is O(1).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- In an AVL tree a single node has height 0. What is the minimum number of nodes in an AVL tree of height 6?
- int f(int n) { static int c = 0; c += n; if (n > 0) f(n - 1); return c; } int main() { int x = f(4); int y = f(2); printf("%d", x + y); }…
- Assume a 32-bit unsigned int. What does the following program print? #include <stdio.h> int main() { unsigned s = 0; for (unsigned i = 1…
- Assume uint16t is a 16-bit unsigned type, int is 32 bits and the characters are ASCII. What is printed? uint16t h = 7; for (const char p =…
- int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a b; } int main() { int…
- Hoare partition on a[0..8] = {8, 3, 9, 1, 8, 5, 2, 7, 6}: int hoare(int a[], int lo, int hi) { int p = a[lo], i = lo - 1, j = hi + 1…