Java coding interview

Searching & Sorting Questions for FAANG Interviews 2026

Searching and sorting problems form the backbone of FAANG technical interviews (Facebook/Meta, Amazon, Apple, Netflix, Google). These questions test not only your coding ability but also your problem-solving approach, optimization skills, and understanding of algorithms.

In this article, we cover the most important searching and sorting questions asked in FAANG interviews, organized by difficulty level, along with explanations of what interviewers expect.

Why FAANG Focuses on Searching & Sorting

FAANG companies heavily rely on:

  • Efficient data retrieval
  • Large-scale data processing
  • Performance-critical systems

Searching and sorting problems help interviewers evaluate:

  • Algorithmic thinking
  • Binary search mastery
  • Time & space complexity awareness
  • Edge case handling

Level 1: Core Searching Foundations (Must-Clear)

1. Binary Search in a Sorted Array

Problem:
Search a target element in a sorted array.

Binary Search is a searching technique used on a sorted array. Instead of checking elements one by one, it:

  • Always checks the middle element
  • Discards half of the array every step

That’s why it’s very fast.

What Interviewers Test

  • Correct mid calculation
  • Loop termination conditions
  • Handling element not found

Expected Complexity

  • Time: O(log n)
  • Space: O(1)

2. Find First and Last Occurrence of an Element

Example

Input  : [1,2,2,2,3,4], target = 2
Output : [1,3]

Key Concept

  • Modified binary search
  • Left bias and right bias

FAANG Note:
Amazon and Meta frequently ask this.

What candidate try first:  (Linear)

import java.util.*;
class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1,2,2,2,3,4);
        List<Integer> positions = new ArrayList<>();
        System.out.println(numbers);
        for(int i=0; i<numbers.size(); i++) {            
        if(numbers.get(i)==2 && positions.indexOf(numbers.get(i)) == -1) {
                positions.add(i);
        } else if(numbers.get(i)==2 && positions.indexOf(numbers.get(i)) >=0) {
                positions.set(1, i);
        } else {
            continue;
        }
        }
        
           System.out.println(positions);       
    }
}

Positions.set it overwrite the value at index position.
Positions.add it add value at index position, and shift existing to the right or next index.

Above Brute Force Approach (Not Recommended)

  • Traverse the array linearly
  • Record first and last occurrence
  • Time Complexity: O(n)
  • Rejected in FAANG interviews
Above code AspectValue
TechniqueLinear Search
Array typeSorted / Unsorted
Time ComplexityO(n)
FAANG acceptable?❌ No

Binary Search Approach

Below Code AspectValue
TechniqueModified Binary Search
Array typeSorted
Time ComplexityO(log n)
FAANG acceptable?✅ Yes

class Main {
    public static void main(String[] args) {
        int[] nums = {1, 2, 2, 2, 3, 4};
        int target = 2;

        int[] result = searchRange(nums, target);

        System.out.println("First Occurrence: " + result[0]);
        System.out.println("Last Occurrence : " + result[1]);
    }

    public static int[] searchRange(int[] nums, int target) {
        int first = findFirst(nums, target);
        int last = findLast(nums, target);
        return new int[]{first, last};
    }

    // Left-biased Binary Search (First Occurrence)
    private static int findFirst(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        int result = -1;

        while (low <= high) {
            int mid = low + (high - low) / 2;

            if (nums[mid] == target) {
                result = mid;
                high = mid - 1; // move left
            } else if (nums[mid] < target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return result;
    }

    // Right-biased Binary Search (Last Occurrence)
    private static int findLast(int[] nums, int target) {
        int low = 0, high = nums.length - 1;
        int result = -1;

        while (low <= high) {
            int mid = low + (high - low) / 2;

            if (nums[mid] == target) {
                result = mid;
                low = mid + 1; // move right
            } else if (nums[mid] < target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return result;
    }
}

3. Search Insert Position

Problem:
Return the index where a target should be inserted to keep array sorted.

Given a sorted array of integers and a target value, return the index where the target is found. If the target is not present, return the index where it should be inserted to keep the array sorted.

Example

Example 1

Input  : nums = [1,3,5,6], target = 5
Output : 2

Example 2

Input  : nums = [1,3,5,6], target = 2
Output : 1

Example 3

Input  : nums = [1,3,5,6], target = 7
Output : 4

Code:

class Main {

    public static void main(String[] args) {

        int[] nums = {1,3,5,6};
        int target = 7;
        int result = searchInsert(nums, target);

        System.out.println("index: " + result);
    }

  public static int searchInsert(int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;

            if (nums[mid] == target) {
                return mid;
            } 
            else if (nums[mid] < target) {
                low = mid + 1;
            } 
            else {
                high = mid - 1;
            }
        }

        // insertion position
        return low;
    }
  
  
}

4. Find Missing Number in a Sorted Array

Problem:
One number missing from a sorted range.

Follow-Up Question

  • Can you solve it in O(log n)?

Level 2: Sorting Logic & Array Manipulation

5. Sort an Array of 0s, 1s, and 2s

(Dutch National Flag Problem)

Constraints

  • No built-in sort
  • No extra space

Key Concept

  • Three-pointer technique

6. Merge Two Sorted Arrays In-Place

Problem:
Merge two sorted arrays without using extra space.

Tests

  • Index manipulation
  • Space optimization

7. Find Kth Largest / Smallest Element

Approaches Expected

  • Sorting (O(n log n))
  • Heap (O(n log k))
  • QuickSelect (O(n) average)

Follow-Up

Why is QuickSelect better than sorting?

8. Check if Array Is Almost Sorted

Condition

  • Array can be sorted using one swap or one reverse

Tests

  • Observation skills
  • Sorting patterns

Level 3: Binary Search on Answer (FAANG Favorite)

9. Square Root of a Number (Without Math Library)

Concept

  • Binary search on solution space

Tests

  • Precision handling
  • Integer boundaries

10. Minimum Element in Rotated Sorted Array

Example

Input  : [4,5,6,7,0,1,2]
Output : 0

Follow-Ups

  • What if duplicates exist?
  • Worst-case complexity?

11. Search in Rotated Sorted Array

Key Skill

  • Identifying sorted half
  • Conditional binary search

Very common Google & Amazon question

12. Find Peak Element

Definition

  • Element greater than its neighbors

FAANG Insight

  • Multiple correct answers allowed
  • Reasoning is more important than result

Level 4: Advanced FAANG-Level Problems

13. Median of Two Sorted Arrays

Constraints

  • Time: O(log(min(n, m)))

Why FAANG Loves This

  • Deep binary search understanding
  • Partition logic

14. Count Inversions in an Array

Definition

  • Measure of how unsorted an array is

Key Technique

  • Merge sort with counting

15. Sort Characters by Frequency

Example

Input  : "tree"
Output : "eert"

Concepts Used

  • HashMap
  • Sorting or Heap

16. Smallest Missing Positive Number

Constraints

  • Time: O(n)
  • Space: O(1)

Classic FAANG Trick Question

How FAANG Evaluates Your Solution

Interviewers focus on:

  • Clear explanation
  • Correct edge case handling
  • Optimal complexity
  • Clean and readable code
  • Ability to improve brute force solutions

FAANG Interview Preparation Tips

✔ Always start with a brute-force approach
✔ Optimize step by step
✔ Discuss edge cases
✔ State time & space complexity clearly
✔ Ask clarifying questions

Similar Posts