Java Array Problems for Coding Interviews
1. Two Sum
Problem: Find two indices such that their values add up to a given target.
LeetCode Link: https://leetcode.com/problems/two-sum/
Java Code:
import java.util.HashMap;
public class TwoSum {
public static int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int diff = target - nums[i];
if (map.containsKey(diff)) {
return new int[]{map.get(diff), i};
}
map.put(nums[i], i);
}
return new int[0];
}
}
2. Best Time to Buy and Sell Stock
Problem: Maximize profit by choosing a day to buy and sell stock.
LeetCode Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Java Code:
public class BuySellStock {
public static int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;
for (int price : prices) {
if (price < minPrice) minPrice = price;
else maxProfit = Math.max(maxProfit, price - minPrice);
}
return maxProfit;
}
}
3. Maximum Subarray (Kadane's Algorithm)
Problem: Find the contiguous subarray with the largest sum.
LeetCode Link: https://leetcode.com/problems/maximum-subarray/
Java Code:
Java Array Problems for Coding Interviews
public class Kadane {
public static int maxSubArray(int[] nums) {
int current = nums[0], max = nums[0];
for (int i = 1; i < nums.length; i++) {
current = Math.max(nums[i], current + nums[i]);
max = Math.max(max, current);
}
return max;
}
}
4. Move Zeroes
Problem: Move all zeroes to the end, maintaining order of other elements.
LeetCode Link: https://leetcode.com/problems/move-zeroes/
Java Code:
public class MoveZeroes {
public static void moveZeroes(int[] nums) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != 0) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
}
}
5. Contains Duplicate
Problem: Check if the array contains any duplicates.
LeetCode Link: https://leetcode.com/problems/contains-duplicate/
Java Code:
import java.util.HashSet;
public class Duplicate {
public static boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
if (set.contains(num)) return true;
set.add(num);
}
return false;
Java Array Problems for Coding Interviews
}
}
6. Sliding Window Maximum
Problem: Find max in every window of size k.
LeetCode Link: https://leetcode.com/problems/sliding-window-maximum/
Java Code:
import java.util.*;
public class SlidingWindowMax {
public static int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length == 0) return new int[0];
int n = nums.length;
int[] result = new int[n - k + 1];
Deque<Integer> dq = new LinkedList<>();
for (int i = 0; i < n; i++) {
while (!dq.isEmpty() && dq.peek() < i - k + 1) dq.poll();
while (!dq.isEmpty() && nums[dq.peekLast()] < nums[i]) dq.pollLast();
dq.offer(i);
if (i >= k - 1) result[i - k + 1] = nums[dq.peek()];
}
return result;
}
}
7. Subarray Sum Equals K
Problem: Count the number of subarrays with sum equals k.
LeetCode Link: https://leetcode.com/problems/subarray-sum-equals-k/
Java Code:
import java.util.HashMap;
public class SubarraySumK {
public static int subarraySum(int[] nums, int k) {
int count = 0, sum = 0;
HashMap<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for (int num : nums) {
sum += num;
if (map.containsKey(sum - k)) count += map.get(sum - k);
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return count;
}
Java Array Problems for Coding Interviews