Solve Algorithmic Challenges in Online Coding Assessments
Company: Amazon
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in fundamental algorithmic problem-solving, specifically array manipulation, string processing, and the use of basic data structures along with performance and complexity reasoning.
Two Sum
Constraints
- 2 <= len(nums) <= 10^4
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- Exactly one valid answer exists.
Examples
Input: ([2, 7, 11, 15], 9)
Expected Output: [0, 1]
Explanation: nums[0] + nums[1] = 2 + 7 = 9.
Input: ([3, 2, 4], 6)
Expected Output: [1, 2]
Explanation: nums[1] + nums[2] = 2 + 4 = 6; note nums[0]=3 alone cannot reuse itself.
Input: ([3, 3], 6)
Expected Output: [0, 1]
Explanation: Duplicate values; the two distinct indices 0 and 1 sum to 6.
Input: ([-1, -2, -3, -4, -5], -8)
Expected Output: [2, 4]
Explanation: Negatives: nums[2] + nums[4] = -3 + -5 = -8.
Input: ([0, 4, 3, 0], 0)
Expected Output: [0, 3]
Explanation: Two zeros at indices 0 and 3 sum to the target 0.
Hints
- A brute-force O(n^2) approach checks every pair, but you can do better.
- As you iterate, store each number's index in a hash map.
- For each number, check whether target - number is already in the map; if so you have your pair.
Longest Substring Without Repeating Characters
Constraints
- 0 <= len(s) <= 5 * 10^4
- s consists of English letters, digits, symbols, and spaces.
Examples
Input: ("abcabcbb",)
Expected Output: 3
Explanation: The longest unique substring is "abc" with length 3.
Input: ("bbbbb",)
Expected Output: 1
Explanation: Every character repeats; the best window is a single "b".
Input: ("pwwkew",)
Expected Output: 3
Explanation: The longest unique substring is "wke" with length 3.
Input: ("",)
Expected Output: 0
Explanation: Empty string has no characters, so the answer is 0.
Input: ("abba",)
Expected Output: 2
Explanation: After the repeat 'b' then 'a', start must jump correctly; the best is "ab" or "ba" of length 2.
Input: ("dvdf",)
Expected Output: 3
Explanation: Tricky case: the answer is "vdf"; start must not move backward when 'd' repeats.
Input: (" ",)
Expected Output: 1
Explanation: A single space is one unique character, length 1.
Hints
- Maintain a sliding window [start, i] that always contains only unique characters.
- Track the most recent index where each character was seen in a hash map.
- When you encounter a repeat that lies inside the current window, jump start to one past its previous index.