Tip 1 : Consistency is the key, practice atleast 1 question daily.
Tip 2 : Focus on the quality of questions over the quantity of questions.
Tip 3 : Practice in time bound frame. Make sure you beat your time next time you solve the same question.
Tip 1: Add atleast 2 projects, one minor and one major. Know every detail about the project you've added to your resume. Focus on the quality of projects and not the quantity. Don't copy any project, add your originality to the project.
Tip 2: Make sure you add expertise level in front of each skill you mention in your resume. This would give the interviewer an idea of how much knowledge you have about a certain skill.
All the college students were made to sit in college computer labs in the morning time. The round was of 120 minutes. We were provided with pen and paper and there were amazon employees who were there to assist. The round consisted of Code debugging, Aptitude, reasoning OOPs, DBMS, Operating Systems, Computer networks questions and 3 coding questions.



Given two integers x and n, write a function to compute xn. We may assume that x and n are small and overflow doesn’t happen.
Input : x = 2, n = 3
Output : 8
Input : x = 7, n = 2
Output : 49
A straightforward approach that I tried was compute pow(x, n) involves multiplying x by itself n times. This can be achieved efficiently using a basic for loop. But it would fail time constraints.
Employing the Divide and Conquer strategy:
To address the problem, adhere to the following approach:
The problem can be recursively defined as follows:
power(x, n) = power(x, n / 2) * power(x, n / 2); // if n is even
power(x, n) = x * power(x, n / 2) * power(x, n / 2); // if n is odd
And we compute power(x, n / 2) only once. This was the optimised solution O(logN) which passed the tests.



Given a matrix of dimension M * N, where each cell in the matrix can have values 0, 1 or 2 which has the following meaning:
0: Empty cell
1: Cells have fresh oranges
2: Cells have rotten oranges
The task is to the minimum time required so that all the oranges become rotten. A rotten orange at index (i,j ) can rot other fresh oranges which are its neighbors (up, down, left, and right). If it is impossible to rot every orange then simply return -1.
Examples: Input: arr[][C] = { {2, 1, 0, 2, 1}, {1, 0, 1, 2, 1}, {1, 0, 0, 2, 1}};
Output: 2
Explanation: At 0th time frame:
{2, 1, 0, 2, 1}
{1, 0, 1, 2, 1}
{1, 0, 0, 2, 1}
At 1st time frame:
{2, 2, 0, 2, 2}
{2, 0, 2, 2, 2}
{1, 0, 0, 2, 2}
At 2nd time frame:
{2, 2, 0, 2, 2}
{2, 0, 2, 2, 2}
{2, 0, 0, 2, 2}
Brute force - Traverse through all oranges in successive rounds. During each round, move the oranges to adjacent positions of those that became rotten in the previous round. But this would fail the time complexity.
Optimised - The concept employs Breadth First Search (BFS) to track the rotting oranges. Oranges become rotten upon contact with other rotten oranges. This resembles a BFS strategy where the graph is organized into layers or cycles, and the search progresses from lower to higher layers.
In the earlier method, although BFS was utilized in concept, the execution was suboptimal and inefficient. Identifying the elements necessitated traversing the entire matrix. By adopting this more efficient BFS approach, time can be significantly minimized.



Given a sorted array and a number x, find a pair in an array whose sum is closest to x.
Examples:
Input: arr[] = {10, 22, 28, 29, 30, 40}, x = 54
Output: 22 and 30
Input: arr[] = {1, 3, 4, 7, 10}, x = 15
Output: 4 and 10
Brute force - A straightforward approach involves examining each pair and monitoring the pair with the smallest absolute difference between their sum and x. Subsequently, the closest pair is printed. The time complexity of this method amounts to O(n^2).
Binary Search Approach:- A more efficient solution compared to the previous approach involves utilizing Binary Search, leveraging the fact that the given array is sorted.
This round was a core coding interview round with the interviewer. It was meant to test how good is the candidate in problem solving and optimising solutions.



Given an array of size N and an integer K, return the count of distinct numbers in all windows of size K.
Examples:
Input: arr[] = {1, 2, 1, 3, 4, 2, 3}, K = 4
Output: 3 4 4 3
Explanation: First window is {1, 2, 1, 3}, count of distinct numbers is 3
Second window is {2, 1, 3, 4} count of distinct numbers is 4
Third window is {1, 3, 4, 2} count of distinct numbers is 4
Fourth window is {3, 4, 2, 3} count of distinct numbers is 3
Input: arr[] = {1, 2, 4, 4}, K = 2
Output: 2 2 1
Explanation: First window is {1, 2}, count of distinct numbers is 2
First window is {2, 4}, count of distinct numbers is 2
First window is {4, 4}, count of distinct numbers is 1
Brute force -Iterate through the given array, examining each window of size K, and tally the distinct elements within each window. Time complexity was O(N * K2) and interviewer told me to further optimise it.
Optimised using hashing - Count distinct numbers in all windows of size K using hashing.
I was able to solve the problem in O(N) time and O(N) space and interviewer was satisfied.

Given that integers are read from a data stream. Find the median of elements read so far in an efficient way.
There are two cases for median on the basis of data set size.
If the data set has an odd number then the middle one will be consider as median.
If the data set has an even number then there is no distinct middle value and the median will be the arithmetic mean of the two middle values.
Example:
Input Data Stream: 5, 15, 1, 3
Output: 5, 10,5, 4
Explanation: After reading 1st element of stream – 5 -> median = 5
After reading 2nd element of stream – 5, 15 -> median = (5+15)/2 = 10
After reading 3rd element of stream – 5, 15, 1 -> median = 5
After reading 4th element of stream – 5, 15, 1, 3 -> median = (3+5)/2 = 4
I gave the very brute force solution first using Insertion Sort. It's time complexity was O(n2), space O(1).
The interview asked me to further optimise it.
So I gave him the solution using 2 heaps , a min heap and a max heap. The interviewer was satisfied with the solution and asked me to write code for the same. Time complexity O(n*logn) , space - O(n)



Given an array that contains both positive and negative integers, the task is to find the product of the maximum product subarray.
Examples:
Input: arr[] = {6, -3, -10, 0, 2}
Output: 180
Explanation: The subarray is {6, -3, -10}
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60
Explanation: The subarray is {60}
I started with a brute force solution by traversing over every contiguous subarray, finding the product of each of these subarrays and returning the maximum product from these results. Time was O(N^2) and interviewer asked me to optimise it.
Then I gave an optmised solution using Kadane’s algorithm and maintain 3 variables max_so_far, max_ending_here & min_ending_here. Iterate the indices 0 to N-1 and update the variables such that:
max_ending_here = maximum(arr[i], max_ending_here * arr[i], min_ending_here[i]*arr[i])
min_ending_here = minimum(arr[i], max_ending_here * arr[i], min_ending_here[i]*arr[i])
update the max_so_far with the maximum value for each index.
return max_so_far as the result.
The TC for this was reduced to O(N).
The interviewer was happy with this solution and asked me to write neat code for it.



Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(int capacity) Initialize the LRU cache with positive size capacity.int get(int key) Return the value of the key if the key exists, otherwise return -1.
void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
The functions get and put must each run in O(1) average time complexity.
I used Hashmaps & DLL(Doubly Linked List) to solve this.
Intuition:
While inserting the {key,val} pair into the DDL make sure that we are inserting it from the back tail to head.
The cache will tell us when the {key, value} pair is used/inserted.
I was asked general questions, related to my future ambitions and how I would contribute to the company.
Tip 1: Just be honest.
Tip 2: Read about Aamzon's leadership principles.
Tip 3: Don't try to fake anything. The Interviewer is more intelligent than you :)

Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
How do you remove whitespace from the start of a string?