50+ Interview Problems with Solutions
CONTENTS
PART 1: ARRAYS (15 Problems)
PART 2: STRINGS (10 Problems)
PART 3: LINKED LISTS (10 Problems)
PART 4: STACKS & QUEUES (8 Problems)
PART 5: TREES (7 Problems)
PART 1: ARRAYS - 15 PROBLEMS
PROBLEM 1: Two Sum
Difficulty: Easy
Asked By: Amazon, Google, Facebook
Question:
Given array of integers and target, return indices of two numbers that add up to target.
Example:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: nums[0] + nums[1] = 2 + 7 = 9
Solution 1: Brute Force O(n2)
def twoSum(nums, target):
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
Solution 2: Hash Map (Optimal) O(n)
Page 1