Amazon
Porter Interview Questions and Answers
Q1. Given n coins for two players playing a game. Each player picks coins from the given n coins in such a way that he can pick 1 to 5 coins in one turn and the game continues for both the players. The player who p...
read moreThe player who picks the last coin loses the game.
The game starts with n coins.
Each player can pick 1 to 5 coins in one turn.
The player who picks the last coin loses the game.
Determine if the given n coins will result in a win or loss for the starting player.
Q2. Recursive code to reverse a linked list(Handle all corner cases: when list has no nodes or contains a single node)
Reverse a linked list using recursion, handling all corner cases
Create a recursive function that takes the head of the linked list as input
Base case: if the head is null or the list contains only one node, return the head
Recursively call the function with the next node as input and set its next pointer to the current node
Set the current node's next pointer to null and return the new head
Q3. Write a function to check whether a binary tree is a sub-tree of another binary tree (Check for all corner cases)
Write a function to check whether a binary tree is a sub-tree of another binary tree (Check for all corner cases)
Create a function that traverses both trees and compares them
Check if the root of the second tree matches any node in the first tree
Handle cases where one or both trees are empty
Handle cases where the trees have different structures
Q4. Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers
Function to add two numbers represented by linked lists and return the sum list.
Traverse both linked lists and add corresponding nodes, keeping track of carry
Create a new linked list to store the sum
Handle cases where linked lists are of different lengths
Handle cases where the sum has an extra digit due to carry
Return the sum linked list
Q5. Given a sorted array of 0’s and 1’s. Find out the no. of 0’s in it. Write recursive, iterative versions of the code and check for all test cases
Count the number of 0's in a sorted array of 0's and 1's.
Iterative solution: Traverse the array and count the number of 0's.
Recursive solution: Divide the array into two halves and recursively count the number of 0's in each half.
Binary search can also be used to find the first occurrence of 0 and then count the number of 0's after that index.
Q6. Given a string. Write a program to form a string with first character of all words
Program to form a string with first character of all words in a given string.
Split the string into an array of words
Iterate through the array and extract the first character of each word
Join the extracted characters to form the final string
Q7. Given a number n, find the number just greater than n using same digits as that of n
Given a number n, find the number just greater than n using same digits as that of n
Convert the number to a string and sort the digits in ascending order
Starting from the rightmost digit, find the first digit that is smaller than the digit to its right
Swap this digit with the smallest digit to its right that is greater than it
Sort the digits to the right of the swapped digit in ascending order
Concatenate the digits to get the next greater number
Q8. How to build a chess game
To build a chess game, you need to create a board, implement the rules of chess, and allow players to make moves.
Create an 8x8 grid to represent the chess board
Assign pieces to their starting positions on the board
Implement the rules of movement for each type of chess piece (e.g. pawn, rook, bishop)
Allow players to make legal moves and update the board accordingly
Check for checkmate and end the game when one player's king is in checkmate
Q9. Stack for water
A stack for water is a data structure that follows the Last In First Out (LIFO) principle, where elements are added and removed from the top of the stack.
Stack for water can be implemented using an array or a linked list.
When water is poured into a stack, it fills from the top and the last poured water is the first to be removed.
Example: If we pour water into a stack in the order 1, 2, 3, the stack will look like [3, 2, 1].
More about working at Amazon
Interview Process at Porter
Top Junior Software Developer Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month