Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Amazon Coding Interview Questions #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*

Problem Link: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/

*/

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:

TreeNode *MakeBst(vector<int>&nums, int low, int high){
if(low > high)
return NULL;
int middle = low + (high-low)/2;
TreeNode *root = new TreeNode(nums[middle]);
root -> left = MakeBst(nums,low,middle-1);
root -> right = MakeBst(nums,middle+1,high);
return root;
}

TreeNode* sortedArrayToBST(vector<int>& nums) {
if(nums.size()==0)
return NULL;
return MakeBst(nums,0,int(nums.size())-1);
}
};
27 changes: 27 additions & 0 deletions Amazon_Interview_Questions/Easy/Fizz_Buzz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*

Problem Link: https://leetcode.com/problems/fizz-buzz/

*/

class Solution {
public:
vector<string> fizzBuzz(int n) {
vector<string>s;
for(int i=1 ; i<=n ;i++){
if(i%3==0 && i%5!=0){
s.push_back("Fizz");
}
else if(i%3!=0 && i%5==0){
s.push_back("Buzz");
}
else if(i%3==0 && i%5==0){
s.push_back("FizzBuzz");
}
else{
s.push_back(std::to_string(i));
}
}
return s;
}
};
33 changes: 33 additions & 0 deletions Amazon_Interview_Questions/Easy/Lemonade_Change.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*

Problem Link: https://leetcode.com/problems/lemonade-change/

*/


class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int n = bills.size();
if(bills[0]==10)
return false;
int five=0, ten=0;
for(int i=0; i<n; i++){
if(bills[i]==5)
five += 1;
else if(bills[i]==10){
ten += 1;
five -= 1;
}
else if(ten > 0){
ten -= 1;
five -= 1;
}
else
five -= 3;
if(five < 0)
return false;
}
return true;
}
};
27 changes: 27 additions & 0 deletions Amazon_Interview_Questions/Easy/Sqrt(x).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*

Problem Link: https://leetcode.com/problems/sqrtx/

*/


class Solution {
public:
int mySqrt(int x) {
if(x==0) return 0;
int l=0, r=x;

int res, mid; //binary search
while(l<=r){
mid=(l+r)/2;
if(mid == 0) return 1;
if(mid==x/mid) return mid;
else if(mid<x/mid){
l=mid+1;
res = mid;
}
else r=mid-1;
}
return res;
}
};
23 changes: 23 additions & 0 deletions Amazon_Interview_Questions/Medium/House_Robber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*

Problem Link: https://leetcode.com/problems/house-robber/

*/

class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if(n==0)
return 0;
vector<int>a(n);
if(n>=1)
a[0] = nums[0];
if(n>=2)
a[1] = max(nums[0],nums[1]);
for(int i=2; i<n; i++){
a[i] = max(a[i-1],a[i-2]+nums[i]);
}
return a[n-1];
}
};
27 changes: 27 additions & 0 deletions Amazon_Interview_Questions/Medium/Maximum_Product_Subarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*

Problem Link: https://leetcode.com/problems/maximum-product-subarray/

*/

class Solution {
public:
int maxProduct(vector<int>& nums) {
if(nums.size()==0)
return 0;
int maxSub = nums[0];
int minSub = nums[0];
int maxProd = nums[0];
for(int i=1; i<nums.size(); i++){
/* when element is negative we do swap because
when multiplying with a negative integer, number becomes positive so minimum negative numebr will become the maximum number
*/
if(nums[i]<0)
swap(maxSub, minSub);
maxSub = max(maxSub*nums[i],nums[i]);
minSub = min(minSub*nums[i],nums[i]);
maxProd = max(maxProd,maxSub);
}
return maxProd;
}
};
24 changes: 24 additions & 0 deletions Amazon_Interview_Questions/Medium/Perfect_Squares.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*

Problem Link: https://leetcode.com/problems/perfect-squares/

*/

class Solution {
public:
int numSquares(int n) {
if(ceil(sqrt(n)) == floor(sqrt(n)))
return 1;
//legendre's 3-square theorem
while(n%4==0)
n/=4;
if(n%8==7)
return 4;
for(int i=1; i*i<=n; i++){
int base = sqrt(n-i*i);
if(base * base == (n-i*i))
return 2;
}
return 3;
}
};