Amazon Coding Questions

Amazon Coding Questions and Answers

On this page, the 2024  Amazon Coding Questions and Answers are discussed. One of the most crucial rounds for landing a job at Amazon is the coding round.

You can use this section to analyse the level of complexity of the Amazon problem. Understanding the level of complexity and question format for the coding phase is made easier by practising Amazon code questions and answers.

The Amazon Coding Round questions and their solutions in different languages can be found below. For a good grade, you must be prepared for the Amazon code problems.

Coding Test Format

The Amazon coding round is an important step in the interview process.

It assesses a candidate’s problem-solving skills and ability to apply algorithms and data structures to solve coding problems.

  • Coding round contains 3 questions that will have to be attended in 3 hours.
  • Each questions have different difficulty level.
  • There is one Easy problem based on Algorithm , Aptitude and Data structures.
  • One of the problem is of Medium level and that problem is based on Greedy Algorithm.
  • One is of Hard difficulty level, and usually based on Dynamic Programming.
No Of QuestionsMarks per question
Question 120
Question 230
Question 350

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Question 1

Problem Statement: Check if strings are rotations of each other or not
Given two strings s1 and s2. The task is to check if s2 is a rotated version of the string s1. The characters in the strings are in lowercase.

Test Case 1:

Input:
mightandmagic
andmagicmigth

Output:
0

Explanation:
Here with any amount of rotation s2 can’t be obtained by s1.

Your Task:
The task is to complete the function areRotations() which checks if the two strings are rotations of each other. The function returns true if string 1 can be obtained by rotating string 2, else it returns false.

  • Expected Time Complexity: O(N).
  • Expected Space Complexity: O(N).
  • Note: N = |s1|.

Constraints:

  • 1 <= |s1|, |s2| <= 107

Question 2

Problem Statement:

Jarvis is weak in computing palindromes for Alphanumeric characters.While Ironman is busy fighting Thanos, he needs to activate sonic punch but Jarvis is stuck in computing palindromes.You are given a string S containing alphanumeric characters. Find out whether the string is a palindrome or not. If you are unable to solve it then it may result in the death of Iron Man.

Test Case 1:-

Input:
S = “I am :IronnorI Ma, i”

Output:
YES

Explanation:
Ignore all the symbols and whitespaces S = “IamIronnorIMai”. Now, Check for palindrome ignoring uppercase and lowercase English letter.

Test Case 2:-

Input:
S = Ab?/Ba

Output:
YES

Explanation:
Here with any amount of rotation s2 can’t be obtained by s1.

Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function saveIronman() that takes an string (ch), and return the true if the string is a palindrome and false if the string is not a palindrome. The driver code takes care of the printing.

  • Expected Time Complexity: O(N).
  • Expected Space Complexity: O(1).

Note: N = |s1|.

Constraints:

  • 1 ≤ |S| ≤ 100000

Note: Consider alphabets and numbers only for palindrome check. Ignore symbols and whitespaces.

Question 3

Problem Statement:

Given a binary tree, print the bottom view from left to right.

A node is included in bottom view if it can be seen when we look at the tree from bottom.

                    20
                   
/ \
                
8   22
                 /   \   
\
                5  3
  25
                /   \ 

              10
14

For the above tree, the bottom view is 5 10 3 14 25.

If there are multiple bottom-most nodes for a horizontal distance from root, then print the later one in level traversal. For example, in the below diagram, 3 and 4 are both the bottommost nodes at horizontal distance 0, we need to print 4.

                   20
                  
/ \
               
8   22
            
/   \     /   \
          
5  3  4   25
           
  /        \ 
           10          14

For the above tree the output should be 5 10 4 14 25.

Example 1:
Input:

       1
     /   \
    3     2

Output: 3 1 2

Explanation:
First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.

Example 2:
Input:

         10
       /    \
     20    30
     /  \
  40   60

Output: 40 20 60 30

Your Task:
This is a functional problem, you don’t need to care about input, just complete the function bottomView() which takes the root node of the tree as input and returns an array containing the bottom view of the given tree.

  • Expected Time Complexity: O(N).
  • Expected Auxiliary Space: O(N).

Constraints:

  • 1 <= Number of nodes <= 105
  • 1 <= Data of a node <= 105

Question 4

An expression is called the postfix expression if the operator appears in the expression after the operands.

Example :

Infix expression: A + B  *  C – D 

Postfix expression:  A B + C D – *

Given a postfix expression, the task is to evaluate the expression. The answer could be very large, output your answer modulo (10^9+7). Also, use modular division when required.

Note:

  • Operators will only include the basic arithmetic operators like ‘*’, ‘/’, ‘+’, and ‘-‘.
  • The operand can contain multiple digits. 
  • The operators and operands will have space as a separator between them.
  • There won’t be any brackets in the postfix expression.

Input Format:

  • The first line of input contains an integer ‘T’ denoting the number of test cases.
  • The next ‘T’ lines represent the ‘T’ test cases.
  • The first and only line of each test case contains a postfix expression.

Output Format:

  • The first line of input contains an integer ‘T’ denoting the number of test cases.
  • For each test case, print an integer obtained by evaluating the given postfix expression.

Note:

You are not required to print the expected output; it has already been taken care of, Just implement the function.

Constraints
  • 1 <= T <= 100
  • 1 <= N <= 10^3
  • 1 <= NUM <= 100

Where ‘N’ denotes the length of postfix expression and ‘NUM’ denotes the operand.

Time Limit: 1 sec

Sample input 1

2
2 3 1 * + 9 –
1 2 3 + * 8 –

Explanation of sample input 1:

Sample output 1

-4
-3

 

Test case 1:

2 3 1 * + 9 –
– : ( ) – ( )
9 : ( ) – (9)
+ : ( ( ) + ( ) ) – (9)
‘*’:  ( ( ) + ( ( ) * ( ) ) ) – (9)
1 : ( ( ) + ( ( ) * (1) ) ) – (9)
3 : ( ( ) + ( (3) * (1) ) ) – (9)
2 : ( (2) + ( (3) * (1) ) ) – (9) 

Result = (2 + 3) – 9 = 5 – 9 = -4

Test case 2:

1 2 3 + * 8 –
 – : ( ) – ( )
8 : ( ) – (8)
* : ( ( ) * ( ) ) – (8)
+ : ( ( ) * ( ( ) + ( ) ) ) – (8)
3 : ( ( ) * ( ( ) + (3) ) ) – (8)
2 : ( ( ) * ( (2) + (3) ) ) – (8)
1 : ( (1) * ( (2) + (3) ) ) – (8) 

Result = (1 * 5) – 8 = 5 – 8 = -3

Sample input 2

1
100 200 + 2 / 5 * 7 +

Explanation of sample input 2:

100 + 200 = 300
300 / 2 = 150
150 * 5 = 750
750 + 7 = 757

Sample output 2

757

 Question 5

Asked in Infosys Placment Paper – Feb 2022

Problem Statement :

You are given a binary tree, the task is to find out the length of the longest path which contains nodes with the exact same value. It is not necessary for the path to pass through the root of the binary tree.Between two nodes, the length of the path can be defined as the number of edges contained between them.

For example, consider the following binary tree:

                    7

                /     \

               7         7

            /   \         \

           8     3         7

For the above tree, the length of the longest path where each node in the path has the same value is 3 and path is 7 -> 7 -> 7 -> 7

   Input format:

  • The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.
  • The only line of each test case contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 on its place.
  • For example, the input for the tree depicted in the below image would be:

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation:

  • Level 1:
    • The root node of the tree is 1
  • Level 2:
    • Left child of 1 = 2
    • Right child of 1 = 3
  • Level 3:
    • Left child of 2 = 4
    • Right child of 2 = null (-1)
    • Left child of 3 = 5
    • Right child of 3 = 6
  • Level 4:
    • Left child of 4 = null (-1)
    • Right child of 4 = 7
    • Left child of 5 = null (-1)
    • Right child of 5 = null (-1)
    • Left child of 6 = null (-1)
    • Right child of 6 = null (-1)
  • Level 5:
    • Left child of 7 = null (-1)
    • Right child of 7 = null (-1)

The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null(-1).

Note:

The above format was just to provide clarity on how the input is formed for a given tree. The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Output Format:
For each test case, a single integer denoting the length of the longest path where each node in the path has the same value is printed. The output for each test case is to be printed on a separate line.

Note:

You do not need to print anything; it has already been taken care of. Just implement the given function.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 3000
  • -10^9 <= data <= 10^9 and data != -1

 Where ‘T’ is the number of test cases, ‘N’ is the total number of nodes in the binary tree, and “data” is the value of the binary tree node.

Time Limit: 1sec

Sample Input 1:

2
7 7 7 1 1 -1 7 -1 -1 -1 -1 -1 -1
10 3 4 3 3 -1 5 -1 -1 -1 -1 -1 -1

Sample Output 1:

3
2

Sample Input 2:

2
1 4 5 4 4 -1 5 -1 -1 -1 -1 -1 -1
5 4 5 1 1 -1 5 -1 -1 -1 -1 -1 -1

Sample Output 2:

2
2

 Explanation of Sample Input 1:
            7
           / \
          7   7
         / \   \
        1  1    7
For the first test case, the length of the longest path where each node in the path has the same value is 3 and path is 7 -> 7 -> 7 -> 7.

          10
           / \
         3   4
         / \   \
        3  3    5
For the second test case, the length of the longest path where each node in the path has the same value is 2 and path is 3 -> 3 -> 3.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription