Alex
BAN USER
abishek, maybe I'm missing something or misunderstanding the question.
In the second output how can there be 5 total steps when the broken step is at 1?
Shouldn't the output be the total steps you can take before you reach the broken step? My impression was that you cannot go at or beyond the broken step.
Yikes!
Alright here's another way of handling this problem.
Let's pretend there are 20 actual physical items left in stock. I'd report in the amazon UI that there are only 10 items left in stock. Once this count drops to zero, we'd have a backup of 10 more items left on shelf just to handle the scenarios where two customers have the same item in the same cart. This would mitigate the problem but not totally eliminate it. But something along these lines should be sufficient for most scenarios I believe.
Yeah I figured that it wasn't the type of answer the interviewer was looking for.
Okay so let's try to solve this another way.
So based upon this info then the item becomes unavailable only after a customer has clicked on the "Pay" button. Until then it must remain in both customers shopping carts. Once one of the customers clicks on the pay button, the item should be silently removed from the other customer's shopping cart with a message "Sorry this item is no longer available... more on the way" or something of that nature.
I see a lot of brute force like methods. Although my answer is probably not what the interviewer is looking for it can be solved much simpler.
string sumBinary(string s1,string s2)
{
string sum;
int result = (std::stoull(s1.c_str(),NULL,2) + std::stoull(s2.c_str(),NULL,2));
while(result != 0)
{
if(result & 0x1)
{
sum = "1" + sum;
}else
{
sum = "0" + sum;
}
result >>= 1;
}
return sum;
}
void wordsearch(string sWord,vector<string> v,int x,int y,vector<string> vCoord)
{
if(y < v.size() && x < v[y].length())
{
if(sWord[vCoord.size()] == v[y][x])
{
stringstream ss;
ss << v[y][x];
ss << " (" << x << "," << y << ")";
vCoord.push_back(ss.str());
if(vCoord.size() == sWord.length())
{
for(int i=0;i<vCoord.size();i++)
printf("%s ",vCoord[i].c_str());
vCoord.clear();
}
}else
{
vCoord.clear();
}
wordsearch(sWord,v,x+1,y,vCoord);
wordsearch(sWord,v,x,y+1,vCoord);
wordsearch(sWord,v,x+1,y+1,vCoord);
}
}
void additive(int n)
{
stringstream ss;
ss << n;
string s = ss.str();
int substrLen = 1;
int index=0;
while((substrLen * 3) <= s.length())
{
while(index < s.length())
{
string op1 = s.substr(index,substrLen);
string op2 = s.substr(index+op1.length(),substrLen);
string op3 = s.substr(index+op1.length() + op2.length(),substrLen);
if(stoi(op1) + stoi(op2) == stoi(op3))
printf("\n%s + %s = %s",op1.c_str(),op2.c_str(),op3.c_str());
index += substrLen*3;
}
index = 0;
substrLen++;
}
}
bool wildmatch(string s,string sPattern)
{
if(s.length() == 1 && sPattern.length() == 1 && s[0] == sPattern[0])
return true;
if(s.length() == 1 || sPattern.length() == 1)
return false;
if((s[0] == sPattern[0]) || sPattern[0] == '?')
{
return wildmatch(s.substr(1),sPattern.substr(1));
}
else if(sPattern[0] == '*' && s[0] != sPattern[1])
{
return wildmatch(s.substr(1),sPattern);
}else if(sPattern[0] == '*' && s[0] == sPattern[1])
{
return wildmatch(s.substr(1),sPattern.substr(2));
}
return false;
}
We can solve this using recursion.
Call "printTotalReportCount" which will call getDirectReportCount and recursively find all the managers subordinate total.
int getDirectReportCount(map<string,string> mEmployees,string employee,int count)
{
map<string,string>::iterator iter = mEmployees.begin();
while(iter != mEmployees.end())
{
if(iter->second == employee && iter->first != iter->second)
{
count++;
count += getDirectReportCount(mEmployees,iter->first,0);
}
++iter;
}
return count;
}
void printTotalReportCount(map<string,string> mEmployees)
{
map<string,string>::iterator iter = mEmployees.begin();
while(iter != mEmployees.end())
{
printf("\n%s - %d",iter->first.c_str(),getDirectReportCount(mEmployees,iter->first,0));
iter++;
}
}
We can solve this using recursion.
First call is "printTotalReportCount" which will tally every employee's total subordinate count.
int getDirectReportCount(map<string,string> mEmployees,string employee,int count)
{
map<string,string>::iterator iter = mEmployees.begin();
while(iter != mEmployees.end())
{
if(iter->second == employee && iter->first != iter->second)
{
count++;
count += getDirectReportCount(mEmployees,iter->first,0);
}
++iter;
}
return count;
}
void printTotalReportCount(map<string,string> mEmployees)
{
map<string,string>::iterator iter = mEmployees.begin();
while(iter != mEmployees.end())
{
printf("\n%s - %d",iter->first.c_str(),getDirectReportCount(mEmployees,iter->first,0));
iter++;
}
}
The solution is:
1. Pick a position on the grid
2. Fill the grid with all the ways a queen can move,
3. Pick the next empty position
4. Repeat steps 2 and 3.
Once the grid is filled we see if we used N queens and increment the number of ways N queens can be positioned on the grid. We then clear the grid and repeat steps 1 - 4 again with the next position on the grid.
Sample:
N = 10
Output = 1
const int N = 10;
void fillQueenPositions(int row,int col,int (&grid)[N][N],int &count)
{
if(count == N)
return;
if(row < 0 || col < 0)
return;
if(row >= N || col >= N)
return;
//fill horizontal/vertical planes
for(int i = row+1;i<N; i++)
{
grid[i][col] = 2;
}
for(int i = row-1;i>=0; i--)
{
grid[i][col] = 2;
}
for(int i = col+1;i<N; i++)
{
grid[row][i] = 2;
}
for(int i = col-1;i>=0; i--)
{
grid[row][i] = 2;
}
//fill diagonals
int r=row+1,c=col+1;
while(r<N && c<N)
{
grid[r][c] = 2;
r++;
c++;
}
r=row-1,c=col-1;
while(r>=0 && c>=0)
{
grid[r][c] = 2;
r--;
c--;
}
r=row-1,c=col+1;
while(r>=0 && c<N)
{
grid[r][c] = 2;
r--;
c++;
}
r=row+1,c=col-1;
while(r<N && c>=0)
{
grid[r][c] = 2;
r++;
c--;
}
//fill knight move positions
if(row+2 < N && col+1<N)
{
grid[row+2][col+1] = 2;
}
if(row+2 < N && col-1>=0)
{
grid[row+2][col-1] = 2;
}
if(row+1 < N && col+2 < N)
{
grid[row+1][col+2] = 2;
}
if(row+1 < N && col-2 >=0)
{
grid[row+1][col-2] = 2;
}
if(row-2 >=0 && col-1>=0)
{
grid[row-2][col-1] = 2;
}
if(row-2 >=0 && col+1<N)
{
grid[row-2][col+1] = 2;
}
if(row-1 >=0 && col-2>=0)
{
grid[row-1][col-2] = 2;
}
if(row-1 >=0 && col+2<N)
{
grid[row-1][col+2] = 2;
}
grid[row][col] = 1;
count++;
bool bEmptySpotFound = false;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(grid[j][i] == 0)
{
row = j;
col = i;
bEmptySpotFound = true;
break;
}
}
if(bEmptySpotFound)
break;
}
if(bEmptySpotFound)
{
fillQueenPositions(row,col,grid,count);
}
}
int countQueenPositions()
{
int count = 0;
int grid[N][N];
for(int r=0;r<N;r++)
{
for(int c=0;c<N;c++)
{
int nQueens = 0;
fillQueenPositions(r,c,grid,nQueens);
if(nQueens == N)
{
count++;
}
//reset the grid
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
grid[i][j] = 0;
}
}
}
return count;
}
bool isMatch(const char *s,const char *p)
{
if(s == NULL || p == NULL)
return false;
int index=0;
int patternIndex=0;
char lastChar;
for(patternIndex=0;patternIndex<strlen(p) && index<strlen(s);patternIndex++)
{
if(p[patternIndex] == s[index])
{
lastChar = p[patternIndex];
index++;
continue;
}
if(p[patternIndex] == '*')
{
while(index<strlen(s))
{
if(s[index] != lastChar)
{
break;
}
index++;
}
}
else if(p[patternIndex] == '.')
{
lastChar = s[index];
index++;
}else
{
lastChar = p[patternIndex];
}
}
return (patternIndex == strlen(p) && index == strlen(s));
}
EXACTLY... this is what I was confused about. When I ran the test through hacker rank for sample case #1 it came back as failed until I hard coded "nothing".
Also agreed with your analysis of the second example. The answer should be a not b.
I think the engineers at Fuze that setup this test made some mistakes.
In the end I got screwed.
I believe with a palidrome there can only be one (or zero) characters of an odd count in the string. The rest of the characters counted in the string have to be even.
bool canBePalidrome(string s)
{
map<char,int> mCount;
for(int i=0;i<s.length();i++)
{
mCount[s[i]]++;
}
map<char,int>::iterator iter = mCount.begin();
bool bOddFound = false;
while(iter != mCount.end())
{
if(iter->second % 2 != 0)
{
if(bOddFound)
return false;
bOddFound = true;
}
iter++;
}
return true;
}
void getCommon(char *s1,char *s2,char *sCommon)
{
for(int i=0;i<strlen(s1);i++)
{
for(int j=0;j<strlen(s2);j++)
{
if(s1[i] == s2[j])
{
bool bFound = false;
for(int k=0; k<strlen(sCommon);k++)
{
if(s1[i] == sCommon[k])
{
bFound = true;
break;
}
}
if(!bFound)
{
sCommon = strcat(sCommon,&s1[i]);
}
break;
}
}
}
}
void printSum(string s)
{
string sNum = "";
int sum= 0;
vector<int> v;
for(int i=0;i<s.length();i++)
{
if(isdigit(s[i]))
{
sNum += s[i];
}else if(sNum.length() > 0)
{
sum += atoi(sNum.c_str());
sNum = "";
}
}
if(sNum.length() > 0)
sum += atoi(sNum.c_str());
printf("\nsum = %d",sum);
}
int findNextSparse(int n)
{
bool bOne = false;
int current = n;
bool bFound = false;
while(!bFound)
{
current = ++n;
while(current != 0)
{
if(current & 0x1 == 1)
{
if(bOne)
{
bFound = false;
break;
}
bOne = true;
}else
{
bOne = false;
}
current = current >> 1;
}
if(current == 0)
bFound = true;
}
return n;
}
This is a horrible interview question.
evaluate what exactly? There is nothing to evaluate.
And if the example is what was given by the interviewer then the question itself has flaws.
The example implies that W = 6
ONE+TWO = THREE
How can ('O' = 2) + ('T' = 4) = 'T'? In this case 2+4 = 6 which would be 'W'
How can
FOUR +FOUR = EIGHT?
Again you have ('O' = 2) + ('O' = 2) = 4 which equals to 'T' and not 'I' or 'G'
This sounds like a hashtable problem with multiple buckets that handle collisions. However, the hashtable will need to be modified. The unique token is the value generated by the hash function.
With the exception that there will only be three buckets per hashtable row (representing small, medium, large compartments).
When the hash token is generated you can look cycle through the three buckets in the hash table to either insert into an empty space or do a swap.
RepDimaOxygen15, Computer Scientist at Headrun Technologies Pvt Ltd
Hi! all my sweets friends My name is Dimo Oxygen! Now I study in Victoria University of Wellington New Zealand ...
Repaileenpankney, photographer at Precious Moments
Confident and dedicated photographer with experience in both professional and freelance photography. I like to explore Black Magic Spell To ...
Repsherykasper, Accountant at A9
Bonjour, je travaille dans une entreprise en tant que caissier. J'habite à Maysville mais je suis en fait d ...
Repmarktrejjo, Data Engineer at Accolite software
I’m Mark.I believe life is too short to be serious all the time, so if you cannot laugh ...
Repriverajenny935, i love my shop piano at xyz
Hello Everyone,My Name is Jenny Rivera .I have been a piano instructor for more than 25 years! I earned ...
Repjuanktait, Blockchain Developer at ADP
I am Juan from Seattle, WA. I am working as a LAN administrator. I love music, reading historical books, and ...
RepOur mission is to provide informative and Self Improvement advice to help people live their lives better set definite goals ...
RepLooking for the best child development center in Charlotte? Pal A Roos provide summer camp for children Charlotte. Our experienced ...
RepVirginialdelmonte, Animator at lostlovebackvashikaran
Have you lost your husband love? And you want to control your husband mind with vashikaran mantra. Guru ji is ...
Repsunita212jain, Android test engineer at Computer Associates
My name is Sunita jain. I am in Chandigarh (India). I am a computer hardware retailer and wholesaler. I also ...
Heapify takes O(1) space complexity. This should work.
- Alex May 05, 2015