PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

These problems evaluate proficiency with array manipulation, frequency and membership reasoning, and algorithmic analysis of time and space complexity when detecting duplicates and locating longest subarrays constrained by distinct values.

  • easy
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Solve array duplicates and two-type subarray

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Coding questions Solve the following array problems. ### Problem 1: Check for duplicates Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and `false` if every element is distinct. **Input:** `nums: int[]` **Output:** `bool` **Constraints (typical):** - `0 <= nums.length <= 1e5` - `-1e9 <= nums[i] <= 1e9` ### Problem 2: Longest subarray with at most two distinct values ("fruit baskets" variant) You are given an array `fruits` where `fruits[i]` is the type of fruit at position `i`. Starting from any position, you can pick one fruit from each consecutive tree moving right, but you can carry **at most two distinct fruit types** total. Return the **maximum number of fruits** you can pick (i.e., the length of the longest contiguous subarray that contains **no more than 2 distinct values**). **Input:** `fruits: int[]` (or `string[]` depending on representation) **Output:** `int` **Constraints (typical):** - `1 <= fruits.length <= 1e5` - Fruit types are hashable (e.g., integers in a reasonable range).

Quick Answer: These problems evaluate proficiency with array manipulation, frequency and membership reasoning, and algorithmic analysis of time and space complexity when detecting duplicates and locating longest subarrays constrained by distinct values.

Part 1: Check for Duplicates

Given an integer array nums, return True if any value appears at least twice in the array, and False if every element is distinct. The order of elements does not matter; you only need to determine whether at least one duplicate exists.

Constraints

  • 0 <= len(nums) <= 100000
  • -1000000000 <= nums[i] <= 1000000000

Examples

Input: [1, 2, 3, 1]

Expected Output: True

Explanation: The value 1 appears more than once.

Input: [1, 2, 3, 4]

Expected Output: False

Explanation: All values are distinct.

Input: []

Expected Output: False

Explanation: An empty array has no duplicates.

Input: [5]

Expected Output: False

Explanation: A single element cannot form a duplicate.

Input: [-10, 7, 0, -10]

Expected Output: True

Explanation: The value -10 appears twice.

Hints

  1. Try keeping track of values you have already seen as you scan the array once.
  2. If you encounter a value that is already in your tracking structure, you can return immediately.

Part 2: Longest Subarray With At Most Two Distinct Values

You are given an array fruits where each integer represents a fruit type. You may start at any index and collect fruits by moving only to the right, taking exactly one fruit from each consecutive position. You can carry at most two distinct fruit types total. Return the maximum number of fruits you can collect, which is the length of the longest contiguous subarray containing no more than two distinct values. If the array is empty, return 0.

Constraints

  • 0 <= len(fruits) <= 100000
  • Fruit types are integers and may repeat
  • An empty array should return 0

Examples

Input: [1, 2, 1]

Expected Output: 3

Explanation: The whole array contains only two distinct values, so the answer is 3.

Input: [0, 1, 2, 2]

Expected Output: 3

Explanation: The longest valid subarray is [1, 2, 2].

Input: [1, 2, 3, 2, 2]

Expected Output: 4

Explanation: The longest valid subarray is [2, 3, 2, 2].

Input: []

Expected Output: 0

Explanation: No trees means no fruits can be collected.

Input: [3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4]

Expected Output: 5

Explanation: The longest valid subarray is [1, 2, 1, 1, 2], which has length 5.

Hints

  1. Think about maintaining a sliding window that always contains at most two distinct fruit types.
  2. Use a frequency map for the current window, and shrink the left side whenever the window contains more than two distinct values.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Datacenter Router Commands - Amazon (hard)
  • Implement Event Filtering and Queue Routing - Amazon (medium)
  • Determine if all courses can be completed - Amazon (medium)
  • Replace Delimited Tokens in a String - Amazon (medium)
  • Minimize Circular Redistribution Cost - Amazon (medium)