Quick reference for interview questions organized by company and topic. Click on any question to expand details.
Move Zeroes to End - Array
Problem Statement
Given an integer array nums, move all zeroes to the end of it while maintaining the relative order of the non-zero elements.
Note: You must do this in-place without making a copy of the array.
Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:
Input: nums = [0]
Output: [0]
Solution - Java
Approach: Two-Pointer Technique
The idea is to keep track of the position where the next non-zero element should be placed. We iterate through the array, and whenever we find a non-zero element, we swap it with the element at the position where the next non-zero should go.
class Solution {
public void moveZeroes(int[] nums) {
// Position where the next non-zero element should be placed
int nonZeroIndex = 0;
// First pass: place all non-zero elements at the beginning
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[nonZeroIndex] = nums[i];
nonZeroIndex++;
}
}
// Second pass: fill the remaining positions with zeros
while (nonZeroIndex < nums.length) {
nums[nonZeroIndex] = 0;
nonZeroIndex++;
}
}
}
Optimized Approach: Swap Only When Needed
class Solution {
public void moveZeroes(int[] nums) {
int left = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0) {
int temp = nums[left];
nums[left] = nums[i];
nums[i] = temp;
left++;
}
}
}
}
Complexity Analysis
- Time Complexity: O(n) - We iterate through the array at most twice
- Space Complexity: O(1) - In-place solution, only using a pointer variable
Key Points
- In-place modification: The solution modifies the array without using extra space
- Maintains relative order: Non-zero elements keep their original relative order
- Two-pointer technique: Using one pointer to track where to place elements
LeetCode Reference
Companies: Nike
Flatten Object - Object | JavaScript
Problem Statement
Given a nested object, flatten it into a single-level object where nested keys are joined with an underscore. Arrays are left as-is — only plain nested objects get flattened.
Example:
Input:
{
name: 'John',
age: 24,
department: {
name: 'Consumer Experience',
section: 'Technical',
branch: {
name: 'Bangalore',
timezone: 'IST'
}
},
company: {
name: 'SAP',
customers: ['Nike', 'Adidas']
},
skills: ['javascript', 'node.js', 'AWS']
}
Output:
{
name: 'John',
age: 24,
department_name: 'Consumer Experience',
department_section: 'Technical',
department_branch_name: 'Bangalore',
department_branch_timezone: 'IST',
company_name: 'SAP',
company_customers: ['Nike', 'Adidas'],
skills: ['javascript', 'node.js', 'AWS']
}
Solution - JavaScript
Approach: Recursive Flattening
function flattenObject(obj, prefix = '') {
const result = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const value = obj[key];
const newKey = prefix ? `${prefix}_${key}` : key;
// If value is an object and not null/array, recurse
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
Object.assign(result, flattenObject(value, newKey));
} else {
result[newKey] = value;
}
}
}
return result;
}
Usage Example
const flattened = flattenObject(input);
console.log(flattened);
// { name: 'John', age: 24, department_name: 'Consumer Experience', ... }
Complexity Analysis
- Time Complexity: O(n) - Where n is the total number of key-value pairs in the nested object
- Space Complexity: O(n) - For the flattened result object
Key Points
- Recursive approach: Traverses nested structures depth-first
- Key construction: Concatenates parent and child keys with underscore
- Arrays are NOT flattened:
typeof [] === 'object', so without the explicit!Array.isArray(value)check, arrays would get wrongly recursed into. This is the detail interviewers are actually testing. - Preserves primitive values: Non-object values (including arrays) are added directly to the result
Companies: Nike
Companies: Nike
Reverse a String - String | JavaScript/Java
Problem Statement
Given a string, reverse it in-place or return a reversed string. The characters should appear in reverse order.
Example 1:
Input: "abcde"
Output: "edcba"
Example 2:
Input: "hello"
Output: "olleh"
Solution - JavaScript
Approach 1: Using Array Methods
function reverseString(str) {
return str.split('').reverse().join('');
}
Approach 2: Using a Loop
function reverseString(str) {
let reversed = '';
for (let i = str.length - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
Approach 3: Using Recursion
function reverseString(str, index = str.length - 1) {
if (index < 0) {
return '';
}
return str[index] + reverseString(str, index - 1);
}
Approach 4: Using Spread Operator
function reverseString(str) {
return [...str].reverse().join('');
}
Solution - Java
class Solution {
public String reverseString(String s) {
char[] chars = s.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
// Swap characters
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
}
Complexity Analysis
-
Time Complexity:
- Array methods: O(n)
- Loop: O(n)
- Recursion: O(n)
- Spread operator: O(n)
-
Space Complexity:
- Array methods: O(n) - Creating new array
- Loop: O(n) - Creating new string
- Recursion: O(n) - Call stack
- Spread operator: O(n) - Creating new array
Key Points
- Multiple approaches: Different ways to solve depending on requirements
- In-place vs. new string: Consider space constraints
- Two-pointer technique: Efficient for character-level swapping
- String immutability: In Java, strings are immutable, so conversion to char array is needed
LeetCode References
Companies: Nike
Jump Game - Greedy | Array
Problem Statement
Given an array nums where nums[i] is the max jump length from index i, determine if you can reach the last index starting from index 0.
Example:
Input: nums = [2,3,1,1,4]
Output: true
Approach: Greedy — Track Farthest Reachable Index
Keep extending the farthest index you can reach. If your current index ever exceeds the farthest reachable index, you’re stuck.
class Solution {
public boolean canJump(int[] nums) {
int farthest = 0;
for (int i = 0; i < nums.length; i++) {
if (i > farthest) return false;
farthest = Math.max(farthest, i + nums[i]);
}
return true;
}
}
Complexity: O(n) time, O(1) space
Companies: Other
Climbing Stairs - Dynamic Programming
Problem Statement
You’re climbing a staircase of n steps. Each time you can climb 1 or 2 steps. In how many distinct ways can you reach the top?
Example:
Input: n = 4
Output: 5 // (1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2)
Approach: DP — Same Pattern as Fibonacci
Ways to reach step n = ways to reach n-1 + ways to reach n-2.
class Solution {
public int climbStairs(int n) {
if (n <= 2) return n;
int prev2 = 1, prev1 = 2;
for (int i = 3; i <= n; i++) {
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return prev1;
}
}
Complexity: O(n) time, O(1) space
Companies: Other
Sliding Window Maximum - Deque | Array
Problem Statement
Given an array nums and window size k, return the maximum of each sliding window as it moves left to right.
Example:
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
Approach: Monotonic Deque (indices, decreasing values)
Keep indices in the deque whose values are in decreasing order. The front of the deque is always the max for the current window.
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> deque = new ArrayDeque<>(); // stores indices
int[] result = new int[nums.length - k + 1];
int ri = 0;
for (int i = 0; i < nums.length; i++) {
if (!deque.isEmpty() && deque.peekFirst() < i - k + 1) {
deque.pollFirst();
}
while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) {
deque.pollLast();
}
deque.offerLast(i);
if (i >= k - 1) {
result[ri++] = nums[deque.peekFirst()];
}
}
return result;
}
}
Complexity: O(n) time, O(k) space — each index enters/leaves the deque once
Companies: Other
Top K Largest Sum Subarrays - Array | Heap
Problem Statement
Given an array nums and an integer k, find the sums of the top k contiguous subarrays with the largest sums.
Example:
Input: nums = [1,2,3], k = 2
Output: [6,5] // [1,2,3]=6, [2,3]=5
Approach: Min-Heap of Size K
Generate subarray sums and keep only the top k, using a min-heap — pop the smallest whenever the heap grows beyond k.
class Solution {
public List<Integer> topKSumSubarrays(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int i = 0; i < nums.length; i++) {
int sum = 0;
for (int j = i; j < nums.length; j++) {
sum += nums[j];
minHeap.offer(sum);
if (minHeap.size() > k) minHeap.poll();
}
}
List<Integer> result = new ArrayList<>(minHeap);
result.sort(Collections.reverseOrder());
return result;
}
}
Complexity: O(n² log k) time, O(k) space — fine to discuss as-is; mention prefix sums as the optimized follow-up if pushed
Companies: Other
Find the Kth Most Frequent Element - HashMap | Heap
Problem Statement
Given an array (or a table of error logs), find the element with the k-th highest frequency — e.g., the 3rd most frequent error type.
Example:
Input: nums = [1,1,1,2,2,3,3,3,3,4], k = 3
Output: 2 // freq: 3→4, 1→3, 2→2, 4→1 → 3rd highest is 2
Approach: Count with HashMap, then Min-Heap of Size K
class Solution {
public int kthMostFrequent(int[] nums, int k) {
Map<Integer, Integer> freq = new HashMap<>();
for (int num : nums) freq.merge(num, 1, Integer::sum);
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
for (var entry : freq.entrySet()) {
minHeap.offer(new int[]{entry.getKey(), entry.getValue()});
if (minHeap.size() > k) minHeap.poll();
}
return minHeap.peek()[0];
}
}
If asked as SQL (e.g., “3rd most frequent error in the logs table”):
SELECT error_type, COUNT(*) AS freq
FROM error_logs
GROUP BY error_type
ORDER BY freq DESC
LIMIT 1 OFFSET 2; -- 3rd most frequent (0-indexed offset)
Complexity: O(n log k) time, O(n) space
Companies: Nike
Extract Integers from an Array of Strings - String | Array
Problem: Array has a mix of numeric and non-numeric strings. Return only the ones that are integers, as numbers.
Input: ["1", "two", "3", "four", "5"]
Output: [1, 3, 5]
Approach: Filter with a regex, then convert.
function extractIntegers(arr) {
return arr.filter(s => /^-?\d+$/.test(s)).map(Number);
}
extractIntegers(["1", "two", "3", "four", "5"]); // [1, 3, 5]
Companies: EPAM
Find Missing Number in 1 to N - Array | Math
Problem: Array has numbers 1 to n with one missing. Find it.
Input: nums = [1,2,4,5], n = 5
Output: 3
Approach: Expected sum n*(n+1)/2 minus actual sum.
int findMissing(int[] nums, int n) {
int expectedSum = n * (n + 1) / 2;
int actualSum = 0;
for (int num : nums) actualSum += num;
return expectedSum - actualSum;
}
Companies: EPAM
Longest Substring Without Repeating Characters - String | Sliding Window
Problem: Find the length of the longest substring with no repeating characters.
Input: s = "abcncabca"
Output: 4 // "abcn" or "ncab"
Approach: Sliding window, track last-seen index of each char; shrink window when a repeat falls inside it.
int lengthOfLongestSubstring(String s) {
Map<Character, Integer> lastSeen = new HashMap<>();
int start = 0, maxLen = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (lastSeen.containsKey(c) && lastSeen.get(c) >= start) {
start = lastSeen.get(c) + 1;
}
lastSeen.put(c, i);
maxLen = Math.max(maxLen, i - start + 1);
}
return maxLen;
}
Companies: EPAM
3Sum Closest to Target - Array | Two Pointers
Problem: Find the sum of 3 elements closest to a target value.
Input: nums = [-1,2,1,-4], target = 1
Output: 2 // (-1 + 2 + 1)
Approach: Sort, then fix one element and two-pointer the rest — same skeleton as 3Sum, but track closest diff instead of exact-zero.
int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int closestSum = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1, right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (Math.abs(sum - target) < Math.abs(closestSum - target)) {
closestSum = sum;
}
if (sum < target) left++;
else if (sum > target) right--;
else return sum; // exact match, can't get closer
}
}
return closestSum;
}
Companies: EPAM