Data Structures & AlgorithmsModerateProduct Company SDE pattern

What is printed? (two-pointer: remove all occurrences of a value in place)

What is printed? (two-pointer: remove all occurrences of a value in place)
vector<int> a = {3, 2, 2, 3, 4, 3, 5};
int w = 0;
for (int r = 0; r < a.size(); r++)
    if (a[r] != 3) a[w++] = a[r];
cout << w << " " << a[0] << a[1] << a[2] << a[3] << " " << a[4] << a[5] << a[6];
  1. A. 4 2245 345
  2. B. 3 2245 435
  3. C. 4 2245 435
  4. D. 4 2245 000

Correct answer: C. 4 2245 435

The write pointer w copies only the kept values: 2, 2, 4, 5 → length 4. The slots after w are NOT cleared – they still hold the old values 4, 3, 5 from positions 4..6. Callers must use only the first w elements. O(n), O(1).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic