Q: Given an array of integers, return the sum of all elements.
Example: [1, 2, 3, 4] → 10
#include <iostream>
#include <vector>
using namespace std;
int sumArray(const vector<int>& nums) {
int sum = 0;
for (int num : nums) {
sum += num;
}
return sum;
}
int main() {
vector<int> arr = {1, 2, 3, 4};
cout << "Sum: " << sumArray(arr) << endl; // Output: 10
return 0;
}
A fair six-sided die is rolled. What is the probability of rolling a number greater than 4?
2/6 or 1/3
What should you always tailor for each job application?
Resume
What's wrong with this code?
int arr[3] = {1, 2, 3};
cout << arr[3];
Out-of-bounds access (valid indices: 0–2)
How many bits are in a byte?
8
Given a string, determine if it is a palindrome (reads the same forwards and backwards).
Example: "racecar" → true, "hello" → false
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(const string& s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) return false;
left++;
right--;
}
return true;
}
int main() {
string str = "racecar";
cout << (isPalindrome(str) ? "True" : "False") << endl; // Output: True
return 0;
}
What is the derivative of f(x)=3x2+5x−2?
f'(x)=6x+5
What data structure is most commonly asked about in interviews?
Arrays
What's wrong with this code?
#include <iostream>
using namespace std;
int main() {
int a;
cout << a;
return 0;
}
Variable a is uninitialized. Printing it leads to undefined behavior.
What does “rubber duck debugging” mean?
Explaining your code step-by-step to find bugs (typically to a rubberduck)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
Example: [2, 7, 11, 15], target = 9 → [0, 1]
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
vector<int> twoSum(const vector<int>& nums, int target) {
unordered_map<int, int> map; // number -> index
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (map.find(complement) != map.end()) {
return {map[complement], i};
}
map[nums[i]] = i;
}
return {};
}
int main() {
vector<int> nums = {2, 7, 11, 15};
int target = 9;
vector<int> result = twoSum(nums, target);
cout << result[0] << ", " << result[1] << endl; // Output: 0, 1
return 0;
}
How many ways can you arrange the letters in the word “CODE”?
4! = 24
How would you determine the time complexity of a piece of code?
Count how the number of operations grows with input size (loops, nested loops, recursion)
What's wrong with this code?
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i <= 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
The loop condition i <= 5 is out of bounds. The valid indices are 0–4. Should use i < 5.
What is the Big-O time complexity of the following loop?
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << i + j;
}
}
O(n2)
Given a string, return the string reversed.
Example: "hello" → "olleh"
#include <iostream>
#include <string>
using namespace std;
string reverseString(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
swap(s[left], s[right]);
left++;
right--;
}
return s;
}
int main() {
string str = "hello";
cout << reverseString(str) << endl; // Output: olleh
return 0;
}
A fair coin is flipped 3 times. What is the probability of getting exactly 2 heads?
C(3,2) * (0.5)2 * (0.5)1 = 3/8
What STAR stands for in behavioral interview?
Situation, Task, Action, Result
What's wrong with this code?
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i-- ) {
cout << i << " ";
}
return 0;
}
Infinite loop (i is decreasing while condition expects it to increase)
What’s the difference between compiled and interpreted languages?
Compiled = translated before execution; Interpreted = run line-by-line
Given a string, return the index of the first non-repeating character. If it doesn’t exist, return -1.
Example: "leetcode" → 0 (l is first unique)
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int firstUniqChar(string s) {
unordered_map<char, int> freq;
for (char c : s) {
freq[c]++;
}
for (int i = 0; i < s.length(); i++) {
if (freq[s[i]] == 1) return i;
}
return -1;
}
int main() {
string s = "leetcode";
cout << firstUniqChar(s) << endl; // Output: 0
return 0;
}
The dataset is {2, 4, 6, 8, 10}. Compute the variance.
Mean = 6 → Variance = (16+4+0+4+16)/5=8(
What is “time-space tradeoff”?
It’s the idea that you can use more memory to make an algorithm faster or use less memory but slower execution
What's wrong with this code?
#include <iostream>
using namespace std;
int main() {
int* p = nullptr;
cout << *p;
return 0;
}
Dereferencing a null pointer leads to a runtime crash (segmentation fault).
What protocol is primarily used to send emails over the Internet?
SMTP (Simple Mail Transfer Protocol)