Skip to content

Amazon Coding Questions - DSA Problems & Solutions

Practice Amazon placement paper coding questions with detailed solutions. Access Amazon OA coding problems in Java, C++, Python.

This page contains Amazon coding questions from Amazon OA placement papers with detailed solutions.

Amazon OA Coding Section:

  • Problems: 2 coding problems
  • Time: 90 minutes total
  • Languages: Java, C++, Python
Q1: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Solution (Java):

public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{};
}

Time Complexity: O(n)

Question 2: Longest Substring Without Repeating Characters

Section titled “Question 2: Longest Substring Without Repeating Characters”
Q2: Find the length of the longest substring without repeating characters.

Solution (Java):

public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
int maxLen = 0;
int left = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (map.containsKey(c) && map.get(c) >= left) {
left = map.get(c) + 1;
}
map.put(c, right);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}

Time Complexity: O(n)


Practice Amazon coding questions regularly!