GATE Computer ScienceHardGATE CS (PYQ pattern)
A dynamic array starts with capacity 4. When an append finds it full, it allocates capacity c + c / 2 (integer division), copies every existing element and then appends. After 100 appends, how many element copies have been made in total, and what is the capacity?
- A. 124 copies, capacity 128
- B. 182 copies, capacity 141
- C. 278 copies, capacity 211
- D. 278 copies, capacity 141
Correct answer: D. 278 copies, capacity 141
Capacities go 4, 6, 9, 13, 19, 28, 42, 63, 94, 141. A resize happens each time the size reaches the current capacity below 100, copying that many elements: 4 + 6 + 9 + 13 + 19 + 28 + 42 + 63 + 94 = 278, ending at capacity 141. Doubling instead would give 124 copies and capacity 128.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- The words tea, ten, to, inn, in, i, ted, tent, tenant, tens, ton are inserted into an initially empty trie (one node per character, root…
- An array A[-2..5][1..6][0..3] of 4-byte elements has base address 1000 (address of A[-2][1][0]). What is the address of A[3][4][2] in…
- Only the non-zero band of a 20 x 20 tridiagonal matrix (entries with |i - j| <= 1, rows and columns numbered from 1) is stored in a 1-D…
- void f(int n) { if (n <= 1) return; f(n - 1); f(n / 2); } Each activation record takes 32 bytes. What is the peak stack usage in bytes…
- void swap(int x, int y) { x ^= y; y ^= x; x ^= y; } int main() { int a[5] = {3, 7, 9, 12, 5}; for (int i = 0; i < 5; i++) swap(&a[i], &a[4…
- for (i = 2; i <= 100; i++) if (!comp[i]) for (j = i i; j <= 100; j += i) { comp[j] = 1; cnt++; } With comp[] initially all 0, what is the…