GATE Computer ScienceEasyGATE CS (PYQ pattern)
for (i = 1; i < 1000; i *= 2) for (j = 0; j < i; j++) cnt++; What is the final value of cnt (starting from 0)?
- A. 1000
- B. 10000
- C. 1023
- D. 1535
Correct answer: C. 1023
The outer loop runs for i = 1, 2, 4, ..., 512 and the inner loop does i iterations each time: 1 + 2 + ... + 512 = 1023 (a geometric series, so O(n), not O(n log n)).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A table of size 11 uses linear probing with h(k) = k mod 11 and tombstones: a deleted slot is marked DELETED; search continues past…
- The maximum of every window of 3 consecutive elements of [4, 2, 12, 11, -5, 7, 9, 3, 1, 6] is found with a deque of indices: before…
- A singly linked list has 25 nodes. A routine places pointer p at the head and pointer q at the 6th node from the head, then advances both…
- 12-bit sets are stored as bit vectors: A = 101101101101, B = 011010110011, C = 111000011010 (most significant bit first). What are the…
- int f(int a[], int l, int r) { if (l == r) return a[l]; int m = (l + r) / 2; int x = f(a, l, m), y = f(a, m + 1, r); return x > y ? x : y…
- Keys 50, 30, 70, 20, 40, 60, 80, 10, 35, 45, 65, 85, 5, 15, 32, 38, 90 are inserted in this order into an empty BST (root at depth 0)…