Practice Questions — Competitive Programming Patterns in C++
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output?
int n = 16;
cout << ((n & (n - 1)) == 0);n & (n-1) clears the lowest set bit. For powers of 2, this gives 0.
1Question 2
Easy
What is the output?
int arr[] = {3, 1, 3, 1, 5};
int result = 0;
for (int x : arr) result ^= x;
cout << result;XOR: a ^ a = 0, a ^ 0 = a. Pairs cancel out.
5Question 3
Easy
What is the output?
int n = 13; // 1101 in binary
cout << __builtin_popcount(n);__builtin_popcount counts the number of set bits (1s).
3Question 4
Easy
What is the output?
vector<int> v = {1, 2, 3, 4, 5};
int prefix[6] = {0};
for (int i = 0; i < 5; i++)
prefix[i + 1] = prefix[i] + v[i];
cout << prefix[4] - prefix[1];prefix[i] = sum of first i elements. Range sum [1,3] = prefix[4] - prefix[1].
9Question 5
Easy
What is the output?
int n = 10; // 1010
int result = n | (1 << 0); // Set bit 0
cout << result;Setting bit 0 changes 1010 to 1011.
11Question 6
Medium
What is the output?
vector<int> v = {1, 3, 5, 7, 9};
int target = 8;
int lo = 0, hi = v.size() - 1;
while (lo < hi) {
int sum = v[lo] + v[hi];
if (sum == target) {
cout << v[lo] << " " << v[hi];
break;
} else if (sum < target) lo++;
else hi--;
}
if (lo >= hi) cout << "Not found";Two pointers on a sorted array. Start from both ends.
1 7Question 7
Medium
What is the output?
vector<int> arr = {2, 3, 5, 1, 4};
int k = 3;
int sum = 0;
for (int i = 0; i < k; i++) sum += arr[i];
int maxSum = sum;
for (int i = k; i < (int)arr.size(); i++) {
sum += arr[i] - arr[i - k];
maxSum = max(maxSum, sum);
}
cout << maxSum;Sliding window of size 3. Track the maximum sum.
10Question 8
Medium
What is the output?
int n = 42; // 101010
for (int i = 5; i >= 0; i--)
cout << ((n >> i) & 1);Extract each bit from MSB to LSB using right shift and AND with 1.
101010Question 9
Medium
What is the output?
int n = 7; // 0111
int cleared = n & ~(1 << 1); // Clear bit 1
cout << cleared;~(1 << 1) = ~(0010) = 1101. AND with 0111 clears bit 1.
5Question 10
Medium
What is the output?
int a = 5, b = 3;
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout << a << " " << b;XOR swap trick: swaps two variables without a temporary.
3 5Question 11
Hard
What is the output?
vector<int> nums = {1, 2, 3};
int n = nums.size();
for (int mask = 0; mask < (1 << n); mask++) {
cout << "{ ";
for (int i = 0; i < n; i++)
if (mask & (1 << i))
cout << nums[i] << " ";
cout << "} ";
}This generates all subsets using bitmask enumeration.
{ } { 1 } { 2 } { 1 2 } { 3 } { 1 3 } { 2 3 } { 1 2 3 }Question 12
Hard
What is the output?
vector<int> v = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
int maxSum = v[0], currSum = v[0];
for (int i = 1; i < (int)v.size(); i++) {
currSum = max(v[i], currSum + v[i]);
maxSum = max(maxSum, currSum);
}
cout << maxSum;Kadane's algorithm finds the maximum subarray sum in O(n).
6Question 13
Hard
What is the output?
vector<int> nums = {1, 1, 1};
int k = 2;
unordered_map<int, int> prefixCount;
prefixCount[0] = 1;
int sum = 0, count = 0;
for (int x : nums) {
sum += x;
if (prefixCount.count(sum - k))
count += prefixCount[sum - k];
prefixCount[sum]++;
}
cout << count;Prefix sum + hash map to count subarrays with sum == k.
2Question 14
Hard
What is the output?
int n = 0;
for (int i = 0; i < 10; i++)
if (i & 1) n++;
cout << n;i & 1 checks if i is odd (bit 0 is set).
5Question 15
Easy
What is the output?
vector<int> v = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 1; i < (int)v.size(); i++)
sum += v[i] - v[i - 1];
cout << sum;Sum of consecutive differences in an array.
40Question 16
Medium
What is the output?
int n = 255; // 11111111
int count = 0;
while (n) {
count++;
n = n & (n - 1);
}
cout << count;n & (n-1) clears the lowest set bit. Count how many times until n becomes 0.
8Question 17
Medium
What is the output?
vector<int> v = {3, 1, -2, 5, -3, 2};
vector<int> prefix(v.size() + 1, 0);
for (int i = 0; i < (int)v.size(); i++)
prefix[i + 1] = prefix[i] + v[i];
cout << prefix[4] - prefix[1];prefix[i] = sum of first i elements. Range sum v[1..3] = prefix[4] - prefix[1].
4Question 18
Hard
What is the output?
vector<int> v = {1, 3, 5, 7, 9, 11};
int lo = 0, hi = v.size() - 1;
int target = 6;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (v[mid] <= target) lo = mid + 1;
else hi = mid - 1;
}
cout << hi << " " << lo;Binary search variant: find the last element <= target.
2 3Question 19
Easy
What is the output?
int x = 5; // 101
int y = 3; // 011
cout << (x & y) << " " << (x | y);AND keeps common bits. OR combines all bits.
1 7Question 20
Hard
What is the output?
string s = "abcba";
int lo = 0, hi = s.size() - 1;
bool isPalin = true;
while (lo < hi) {
if (s[lo] != s[hi]) { isPalin = false; break; }
lo++; hi--;
}
cout << isPalin;Two pointers from both ends to check palindrome.
1Mixed & Application Questions
Question 1
Easy
What is the output?
int x = 6; // 110
int y = 3; // 011
cout << (x ^ y);XOR returns 1 where bits differ.
5Question 2
Easy
What is the output?
int x = 12;
cout << (x >> 2);Right shift by 2 divides by 4 (for non-negative integers).
3Question 3
Easy
What is the output?
cout << (1 << 10);Left shift by k multiplies by 2^k.
1024Question 4
Medium
What is the output?
vector<int> v = {1, 2, 3, 4, 5, 6};
int lo = 0, hi = v.size() - 1;
int target = 7;
int pairs = 0;
while (lo < hi) {
int sum = v[lo] + v[hi];
if (sum == target) { pairs++; lo++; hi--; }
else if (sum < target) lo++;
else hi--;
}
cout << pairs;Two pointers counting all pairs that sum to target.
3Question 5
Medium
What is the output?
vector<int> v = {10, 20, 30, 40, 50};
vector<int> prefix(6, 0);
for (int i = 0; i < 5; i++)
prefix[i + 1] = prefix[i] + v[i];
// Sum of v[1..3]
cout << prefix[4] - prefix[1];prefix[i] stores sum of first i elements. Range sum = prefix[r+1] - prefix[l].
90Question 6
Medium
What is the output?
int n = 20; // 10100
int toggled = n ^ (1 << 2); // Toggle bit 2
cout << toggled;XOR with 1 toggles a bit. 10100 XOR 00100 = ?
16Question 7
Hard
What is the output?
vector<int> arr = {4, 2, 1, 5, 3};
int k = 2;
int n = arr.size();
int windowMin = INT_MAX;
for (int i = 0; i <= n - k; i++) {
int localMax = *max_element(arr.begin() + i, arr.begin() + i + k);
windowMin = min(windowMin, localMax);
}
cout << windowMin;Find the minimum of maximum values across all windows of size k.
2Question 8
Hard
What is the output?
int n = 15; // 1111
int lowest = n & (-n);
cout << lowest;n & (-n) isolates the lowest set bit.
1Question 9
Hard
What is the output?
int n = 12; // 1100
int lowest = n & (-n);
cout << lowest;n & (-n) isolates the lowest set bit.
4Question 10
Hard
What is the output?
// Greedy: minimum coins for amount
vector<int> coins = {1, 5, 10, 25};
int amount = 41;
int count = 0;
for (int i = coins.size() - 1; i >= 0; i--) {
count += amount / coins[i];
amount %= coins[i];
}
cout << count;Greedy coin change: use the largest coin as many times as possible.
4Question 11
Medium
When does the greedy approach work, and when does it fail?
Think about the greedy choice property.
Greedy works when the problem has the greedy choice property (a locally optimal choice leads to a globally optimal solution) and optimal substructure (optimal solution to the problem contains optimal solutions to subproblems). It works for activity selection, fractional knapsack, Huffman coding, and minimum spanning trees. It fails for 0/1 knapsack, shortest path with negative edges, and the general coin change problem (where coin denominations are not canonical).
Question 12
Hard
Explain the binary search on answer technique. When should you use it?
Think about searching the solution space, not the input.
Binary search on the answer works when: (1) the answer lies in a known range [lo, hi], (2) you can write a function
canDo(mid) that checks if a given answer is feasible, and (3) the feasibility function is monotonic (if mid works, then mid+1 also works, or vice versa). You binary search this range, checking feasibility at each midpoint. Classic examples: minimum maximum pages allocation, minimum days to make m bouquets, Koko eating bananas, and splitting array to minimize the largest sum.Question 13
Easy
What is the output?
int n = 8; // 1000
cout << (n & (n - 1));n & (n-1) clears the lowest set bit.
0Question 14
Medium
What is the output?
vector<int> v = {2, 4, 6, 8, 10};
int lo = 0, hi = v.size() - 1;
int target = 14;
bool found = false;
while (lo < hi) {
int sum = v[lo] + v[hi];
if (sum == target) { found = true; break; }
else if (sum < target) lo++;
else hi--;
}
cout << found << " " << lo << " " << hi;Two pointers: find a pair summing to 14.
1 1 4Question 15
Hard
What is the output?
vector<int> arr = {1, 3, -1, -3, 5, 3, 6, 7};
int k = 3;
vector<int> result;
for (int i = 0; i <= (int)arr.size() - k; i++) {
int mx = *max_element(arr.begin() + i, arr.begin() + i + k);
result.push_back(mx);
}
for (int x : result) cout << x << " ";Maximum of each sliding window of size 3.
3 3 5 5 6 7Question 16
Medium
What is the output?
int a = 10, b = 7;
cout << (a & b) << " " << (a | b) << " " << (a ^ b);10 = 1010, 7 = 0111. Compute AND, OR, XOR.
2 15 13Multiple Choice Questions
MCQ 1
What does ios_base::sync_with_stdio(false) do?
Answer: B
B is correct. It disables the synchronization between
B is correct. It disables the synchronization between
cin/cout and scanf/printf, significantly speeding up C++ I/O. Essential for competitive programming.MCQ 2
What is the result of n & (n - 1) when n is a power of 2?
Answer: C
C is correct. A power of 2 has exactly one set bit. n-1 flips that bit and sets all lower bits. AND produces 0. Example: 8 (1000) & 7 (0111) = 0.
C is correct. A power of 2 has exactly one set bit. n-1 flips that bit and sets all lower bits. AND produces 0. Example: 8 (1000) & 7 (0111) = 0.
MCQ 3
What does XOR of a number with itself give?
Answer: C
C is correct. a ^ a = 0 for any value a. This property is used to find the unique element in arrays where all other elements appear twice.
C is correct. a ^ a = 0 for any value a. This property is used to find the unique element in arrays where all other elements appear twice.
MCQ 4
What is the time complexity of the two pointers technique on a sorted array of size n?
Answer: C
C is correct. Two pointers scan the array from both ends, with each pointer moving at most n times. Total operations: O(n).
C is correct. Two pointers scan the array from both ends, with each pointer moving at most n times. Total operations: O(n).
MCQ 5
What does a sliding window of fixed size k on an array of size n reduce the complexity from?
Answer: B
B is correct. Without sliding window, computing a property for each window of size k takes O(k) per window = O(n*k) total. Sliding window updates incrementally: O(1) per slide = O(n) total.
B is correct. Without sliding window, computing a property for each window of size k takes O(k) per window = O(n*k) total. Sliding window updates incrementally: O(1) per slide = O(n) total.
MCQ 6
What is the time complexity of answering a range sum query using prefix sums (after precomputation)?
Answer: C
C is correct. After building the prefix sum array in O(n), each range sum query is O(1): sum(l, r) = prefix[r] - prefix[l-1].
C is correct. After building the prefix sum array in O(n), each range sum query is O(1): sum(l, r) = prefix[r] - prefix[l-1].
MCQ 7
What does __builtin_popcount(n) return?
Answer: B
B is correct.
B is correct.
__builtin_popcount(n) counts the number of 1-bits in the binary representation of n. It runs in O(1) as a compiler built-in.MCQ 8
In binary search on the answer, what does the feasibility function check?
Answer: B
B is correct. The feasibility function (often called
B is correct. The feasibility function (often called
canDo(mid)) checks whether a given candidate answer is achievable. It typically uses a greedy simulation to verify.MCQ 9
Which bit operation checks if the ith bit of n is set?
Answer: B
B is correct. Right-shift n by i positions to bring bit i to position 0, then AND with 1 to extract it. Result is 0 or 1.
B is correct. Right-shift n by i positions to bring bit i to position 0, then AND with 1 to extract it. Result is 0 or 1.
MCQ 10
What is the greedy strategy for the activity selection problem?
Answer: B
B is correct. Sort activities by end time. Greedily select the next activity that starts after the current one ends. This provably maximizes the number of non-overlapping activities.
B is correct. Sort activities by end time. Greedily select the next activity that starts after the current one ends. This provably maximizes the number of non-overlapping activities.
MCQ 11
For an input of size n = 10^6, which time complexity will likely pass within 1 second?
Answer: C
C is correct. For n = 10^6: O(n log n) = ~20 million operations (passes easily). O(n^2) = 10^12 (far too slow). O(n*sqrt(n)) = ~10^9 (borderline). O(2^n) is astronomical.
C is correct. For n = 10^6: O(n log n) = ~20 million operations (passes easily). O(n^2) = 10^12 (far too slow). O(n*sqrt(n)) = ~10^9 (borderline). O(2^n) is astronomical.
MCQ 12
What is the total number of subsets of a set with n elements?
Answer: B
B is correct. Each element is either included or excluded, giving 2 choices per element. Total subsets: 2^n. Bitmask enumeration uses masks from 0 to 2^n - 1 to generate all subsets.
B is correct. Each element is either included or excluded, giving 2 choices per element. Total subsets: 2^n. Bitmask enumeration uses masks from 0 to 2^n - 1 to generate all subsets.
MCQ 13
What does Kadane's algorithm find?
Answer: B
B is correct. Kadane's algorithm finds the maximum sum contiguous subarray in O(n). At each position, it decides whether to extend the current subarray or start a new one:
B is correct. Kadane's algorithm finds the maximum sum contiguous subarray in O(n). At each position, it decides whether to extend the current subarray or start a new one:
currSum = max(arr[i], currSum + arr[i]).MCQ 14
What is the overflow-safe way to compute the midpoint in binary search?
Answer: B
B is correct.
B is correct.
(lo + hi) / 2 can overflow when lo + hi exceeds INT_MAX. lo + (hi - lo) / 2 avoids this by computing the difference first, which is always non-negative and within range.MCQ 15
What is n & (-n) for a non-zero integer n?
Answer: B
B is correct.
B is correct.
n & (-n) isolates the lowest set bit. For example, 12 (1100) & -12 = 4 (0100). This trick is used in Fenwick trees (Binary Indexed Trees).MCQ 16
What does 1 << n compute?
Answer: C
C is correct. Left-shifting 1 by n positions gives 2^n. For example, 1 << 3 = 8 = 2^3.
C is correct. Left-shifting 1 by n positions gives 2^n. For example, 1 << 3 = 8 = 2^3.
Coding Challenges
Challenge 1: Two Pointers: Remove Duplicates from Sorted Array In-Place
EasyGiven a sorted array, remove duplicates in-place and return the new length. Use two pointers: a slow pointer for the write position and a fast pointer for scanning.
Sample Input
{1, 1, 2, 2, 3, 4, 4, 5}
Sample Output
Unique elements: 5
Array: 1 2 3 4 5
O(n) time, O(1) extra space. Modify the array in-place.
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> v = {1, 1, 2, 2, 3, 4, 4, 5};
if (v.empty()) { cout << 0 << endl; return 0; }
int slow = 0;
for (int fast = 1; fast < (int)v.size(); fast++) {
if (v[fast] != v[slow]) {
slow++;
v[slow] = v[fast];
}
}
int newLen = slow + 1;
cout << "Unique elements: " << newLen << endl;
cout << "Array: ";
for (int i = 0; i < newLen; i++) cout << v[i] << " ";
cout << endl;
return 0;
}Challenge 2: Sliding Window: Count Subarrays with Sum Exactly K
MediumGiven an array of positive integers and a target sum K, count the number of contiguous subarrays whose sum equals exactly K using prefix sums and a hash map.
Sample Input
{1, 2, 3, 4, 1, 2, 3}, K = 6
Sample Output
Subarrays with sum 6: 3
O(n) time complexity. Use prefix sum + hash map pattern.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> arr = {1, 2, 3, 4, 1, 2, 3};
int k = 6;
unordered_map<int, int> prefixCount;
prefixCount[0] = 1;
int sum = 0, count = 0;
for (int x : arr) {
sum += x;
if (prefixCount.count(sum - k))
count += prefixCount[sum - k];
prefixCount[sum]++;
}
cout << "Subarrays with sum " << k << ": " << count << endl;
return 0;
}Challenge 3: Bit Manipulation: Find Two Non-Repeating Elements
HardGiven an array where every element appears twice except two elements that appear once, find those two elements using XOR in O(n) time and O(1) space.
Sample Input
{1, 2, 3, 1, 2, 4}
Sample Output
Two unique elements: 3 4
O(n) time, O(1) space. Use XOR and bit manipulation only.
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> arr = {1, 2, 3, 1, 2, 4};
// Step 1: XOR all elements -> xorAll = a ^ b
int xorAll = 0;
for (int x : arr) xorAll ^= x;
// Step 2: Find any set bit in xorAll (a and b differ here)
int diffBit = xorAll & (-xorAll); // lowest set bit
// Step 3: Partition elements by that bit, XOR each group
int group1 = 0, group2 = 0;
for (int x : arr) {
if (x & diffBit)
group1 ^= x;
else
group2 ^= x;
}
if (group1 > group2) swap(group1, group2);
cout << "Two unique elements: " << group1 << " " << group2 << endl;
return 0;
}Challenge 4: Binary Search on Answer: Koko Eating Bananas
HardKoko has n piles of bananas. She can eat at most K bananas per hour from one pile. If a pile has fewer than K bananas, she finishes that pile and waits. Find the minimum K such that she finishes all bananas within H hours.
Sample Input
piles = {3, 6, 7, 11}, H = 8
Sample Output
Minimum eating speed: 4
Binary search on K in range [1, max(piles)]. Feasibility check: sum of ceil(pile/K) <= H.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canFinish(vector<int>& piles, int k, int h) {
long long hours = 0;
for (int p : piles)
hours += (p + k - 1) / k; // ceil(p / k)
return hours <= h;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> piles = {3, 6, 7, 11};
int H = 8;
int lo = 1, hi = *max_element(piles.begin(), piles.end());
int answer = hi;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (canFinish(piles, mid, H)) {
answer = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
cout << "Minimum eating speed: " << answer << endl;
return 0;
}Challenge 5: Kadane's Algorithm: Maximum Subarray Sum with Indices
MediumFind the maximum sum contiguous subarray and print the start and end indices along with the sum. Use Kadane's algorithm.
Sample Input
{-2, 1, -3, 4, -1, 2, 1, -5, 4}
Sample Output
Max sum: 6
Subarray: index 3 to 6 -> [4, -1, 2, 1]
O(n) time, O(1) space.
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> v = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
int maxSum = v[0], currSum = v[0];
int start = 0, end = 0, tempStart = 0;
for (int i = 1; i < (int)v.size(); i++) {
if (v[i] > currSum + v[i]) {
currSum = v[i];
tempStart = i;
} else {
currSum += v[i];
}
if (currSum > maxSum) {
maxSum = currSum;
start = tempStart;
end = i;
}
}
cout << "Max sum: " << maxSum << endl;
cout << "Subarray: index " << start << " to " << end << " -> [";
for (int i = start; i <= end; i++) {
cout << v[i];
if (i < end) cout << ", ";
}
cout << "]" << endl;
return 0;
}Challenge 6: Prefix Sum: 2D Range Sum Query
HardGiven a 2D matrix, precompute a 2D prefix sum so that any rectangle sum query can be answered in O(1). Compute prefix[i][j] = sum of all elements in the submatrix from (0,0) to (i-1,j-1).
Sample Input
matrix = {{1,2,3},{4,5,6},{7,8,9}}, query: sum of submatrix (1,1) to (2,2)
Sample Output
Sum of submatrix (1,1) to (2,2): 28
O(m*n) precomputation, O(1) per query.
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<vector<int>> matrix = {{1,2,3},{4,5,6},{7,8,9}};
int m = matrix.size(), n = matrix[0].size();
// prefix[i][j] = sum of matrix[0..i-1][0..j-1]
vector<vector<int>> prefix(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
prefix[i][j] = matrix[i-1][j-1] + prefix[i-1][j]
+ prefix[i][j-1] - prefix[i-1][j-1];
// Query: sum of submatrix (r1,c1) to (r2,c2)
int r1 = 1, c1 = 1, r2 = 2, c2 = 2;
int sum = prefix[r2+1][c2+1] - prefix[r1][c2+1]
- prefix[r2+1][c1] + prefix[r1][c1];
cout << "Sum of submatrix (" << r1 << "," << c1 << ") to ("
<< r2 << "," << c2 << "): " << sum << endl;
return 0;
}Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn C++ with a live mentor?
Explore our C++ Masterclass