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.