Top 5 Array Interview Problems in Java (With Real-World Solutions)

7.66K 0 0 0 0

Problem #3: Find the Maximum Product of Two Integers in the Array

🧠 Problem Statement

Given:
An array of integers (can include both positive and negative numbers).

Task:
Find the maximum product that can be obtained by multiplying any two integers in the array.


🧾 Sample Input/Output

Input:
int[] arr = {1, 20, -5, 4, -6};
Output:
120
Explanation:

  • Product of -6 * -5 = 30
  • Product of 20 * 1 = 20
  • Product of -6 * 20 = -120 Maximum = -6 * -5 = 30, but wait — there's a better one: 20 * 6 = 120
    Oops — it should be: 20 * 6 = N/A, but 20 * 4 = 80
    Actually, highest is -6 * -5 = 30
    Sorry! Typo — 20 * 4 = 80, max is 80

Let’s do this clean.


️ Correct Sample:

Input:
int[] arr = {-10, -3, 5, 6, -2}
Output:
60
Explanation:
5 * 6 = 30, -10 * -3 = 30, max is 30


🔍 Brute Force Approach: All Pair Products

public static int maxProductBrute(int[] arr) {

    int max = Integer.MIN_VALUE;

    for (int i = 0; i < arr.length; i++) {

        for (int j = i + 1; j < arr.length; j++) {

            max = Math.max(max, arr[i] * arr[j]);

        }

    }

    return max;

}

Time Complexity: O(n²)
Easy to understand, but slow for large inputs


Optimized Approach: Sort + Pick Extremes

Sort the array, then consider:

  • Product of two largest numbers
  • Product of two smallest (in case both are negative)

import java.util.Arrays;

 

public class MaxProduct {

    public static int maxProduct(int[] arr) {

        Arrays.sort(arr);

 

        int n = arr.length;

        int product1 = arr[0] * arr[1];              // smallest two

        int product2 = arr[n - 1] * arr[n - 2];      // largest two

 

        return Math.max(product1, product2);

    }

 

    public static void main(String[] args) {

        int[] arr = {-10, -3, 5, 6, -2};

        System.out.println(maxProduct(arr)); // Output: 30

    }

}

Time: O(n log n)
Space: O(1) extra (aside from sorting)


🔁 Super Optimized: One-Pass Scan (No Sorting)

public static int maxProductFast(int[] arr) {

    int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE;

    int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;

 

    for (int num : arr) {

        // Max values

        if (num > max1) {

            max2 = max1;

            max1 = num;

        } else if (num > max2) {

            max2 = num;

        }

 

        // Min values

        if (num < min1) {

            min2 = min1;

            min1 = num;

        } else if (num < min2) {

            min2 = num;

        }

    }

 

    return Math.max(max1 * max2, min1 * min2);

}

Time: O(n)
Space: O(1)
No sort required — perfect for interviews


🧠 Edge Cases

Input

Output

Notes

[1, 2]

2

Only one valid product

[-10, -3, 5, 6, -2]

30

Negative * negative is positive

[0, 0, -1, -1]

1

Avoid zeros ruining your logic

[1, 0, 3, 100, -70, -50]

3500

-70 * -50 = 3500 beats 3 * 100 = 300


🧨 Interview Variants

  1. “Find max product of any three numbers”
    👉 Use Math.max(max1 * max2 * max3, min1 * min2 * max1)
  2. “Return the pair that gives the max product”
    👉 Store the pair values during scan
  3. “What if numbers include decimals?”
    👉 Use double[] and modify the same logic

Summary


Approach

Time

Space

Notes

Brute Force

O(n²)

O(1)

Try all pairs

Sort & Compare

O(n log n)

O(1)

Compare min² and max²

One-pass Scan

O(n)

O(1)

Optimal, uses 4 vars only

Back

FAQs


1. Why are array problems so common in Java interviews?

Because they test core programming logic, data handling, loops, and algorithm efficiency.

2. What’s the difference between int[] and Integer[] in Java?

int[] is a primitive array; Integer[] is an array of objects (wrappers). The latter allows null values and works with collections.

3. Is it better to use Arrays or ArrayLists in interviews?

For fixed-size problems, use arrays. For dynamic data, ArrayList is better — but stick to arrays unless otherwise asked

4. What are common pitfalls in Java array questions?

Index out of bounds, mutating arrays while iterating, and forgetting that Java arrays have fixed size.

5. How do I remove duplicates from a Java array?

Use a Set for uniqueness or sort the array first and remove duplicates in-place using two pointers.

6. When should I use the two-pointer technique?

It’s great for sorted arrays, especially for problems involving pair sums, removing duplicates, and reversing data in-place.

7. How can I rotate an array in Java?

Use reversal techniques or cyclic replacements to do it in O(1) space.

8. What’s the time complexity of array search vs binary search?

Linear search = O(n); binary search = O(log n), but only on sorted arrays.

9. How do I handle negative numbers or large input arrays?

Always consider edge cases: empty arrays, one element, all duplicates, etc. Optimize with hashmaps or prefix sums where needed.

10. What libraries or classes help with arrays in Java?

Use Arrays.sort(), System.arraycopy(), Arrays.toString(), and Collections when applicable — but show the manual solution first in interviews.