Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

If string only has characters, assign all the a to z one prime number.
a = 1, b = 2, c = 3, d = 5 ...
Multiply all the characters of string 1.
For eg. abc == 1 * 2 * 3 = 6
and string 2 also.
Find remainder mul(string1) / mul(string2). If it is 0; than string 2 is in string 1, otherwise no.
(Not works for bigger inputs)

- Vabs February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry to say... but ur idea is totally absurd..
example
string1: abcde
string2: abbcdi

- ss March 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ss:i don think the idea is absurd...in ur example..if string 2 is divided by string 1...then it would be val(b)*val(i)/val(e)(remaining cancelled) which will return a floating point as all nos are prime.So the remainder is 0 and will return false as expected...

- user March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ ss : I agree with user. The idea is a great one.Whether it is accepted or not is another thing.But this solution will work .

- code756 October 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume ASCII.
I'd use two arrays T of size 128 and foreach string T[s[i]]=1: O(n + m);
Then match the values from each array. if T1[i] == 1 && T2[i] == 0, the second does not have the (i+1)'th ASCII character; return false(or something).
Return true.

- S February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution:

1. Keep the First string's Characters to a HashSet - O(n)
2. Check for each character from Second String in the HashSet. If any character didn't match, return false - O(m)

Total Time Complexity - O(m+n)
Space Complexity -O(n)

- Karthik February 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Putting n into HashSet O(n2)
check each char from m will O(mn)

- Lokesh February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ignore my comment i miss read it

- Lokesh February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution fails if we have repeated characters in the first string.

- Anonymous June 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

two steps to solve:
1) sort the B string
2) Do a linear search for the characters of A strings

- theStunnerz February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you dont want to sort, solve the problem using associate hash map, and have count for each node(representing the character). do this for string A, and then take char by char from string B, and start decrementing the counts of nodes if the match occurs. After parsing the counts for nodes should be lesser than equal to 0.

- theStunnerz February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create a 26bit array. Now go through source and fill it using string B. Go through A and check if those bits are already set. That will be O(n).

- Kishore February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

of course this is assuming there are only 26 valid characters, and each character is assigned an index.

- Anonymous February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Kishore: some minor modifications. use a array of size 256. and count the number of occurrences for each character in string B.
Ex: A[abcc] B[back], then the answer shud be false. if you use bit array this would be true so use a normal array

- GekkoGordan February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Cant we use the strchr function and code. I think this can be done in O(n) time

- Huntingforintern February 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first sort both string O(log(m+n))
then traverse through second string O(m)

- Lokesh February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If n is small compare to m
put n in Hashmap O(n) then traverse through second string O(m)

total O(m+n)

- Lokesh February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

any one with code for this question?

- Anonymous February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void print(char *mainstr, char *refstring)
{
  int *refstringmap = (int*)malloc(sizeof(int)*26);//just assuming string has only                      alphabets

for(int i=0; *(refstring+i);i++)
{
  refstringmap[*(refstring+i)] = 1;
  refcount++;
}

for(int j=0;*(mainstring+j);j++)
{
  if(refstringmap[*(mainstring+j)]
     maincount++;
}

if(refcount == maincount)
   return true;
else 
   return false;

- crackit February 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry! logic is wrong:(

- crackit March 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this should work:

bool sameChars(const char* p1, const char* p2)
{
  if (!p1 || !p2) return false;
  int aryHit[256] = {0};
  int nCharCount = 0;
  int nSize = strlen(p1);
  for (int i = 0; i < nSize; ++i)
  {
    if (aryHit[p1[i]] == 0) ++nCharCount;
    ++aryHit[p1[i]];
  }
  nSize = strlen(p2);
  int nFoundCount = 0;
  for (int i = 0; i < nSize; ++i)
  {
    if (aryHit[p2[i]] > 0)
    {
      --aryHit[p2[i]];
      if (aryHit[p2[i]] == 0) ++nFoundCount;
      if (nFoundCount == nCharCount)
      {
        return true;
      }
    }
  }
  return false;
}

- Anonymous February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ void fillArr(char arr[], char* Astr) { while(Astr) { arr[*Astr[i]] +=1; - Anonymous February 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is just a pseudocode. It may have syntactical errors.

void fillArr(char countarr[], char* Astr)
{

   while(Astr)
   {
     int i=0;
     countarr[*Astr[i]] +=1;
   }
}

ProcessStr(char* Astr, char*Bstr)
{
   int i=0;
   while(*Bstr+i)
   {
      temp = *Bstr+i;
      if(countarr[temp]>0) countarr[temp]--;
      i++;
   }   
}

Then, check the count array if it has any value for those characters in string A. If it is all 0, then it means those characters were all present in the B string and hence the function would return a true. Else, the function would return a false.

- Anonymous February 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not just use String b.contentEquals(String a) ????

- Anonymous April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Abe gaandu agar inbuild function use karna hota to itna bada discussion kyu ho raha hai yaha ?

- Copyright00 May 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use a hash table....
for each element it has 2 counts...cnt1 and cnt2
initialize both counts with 0
now traverse both strings and put it in hash table and maintain their respective counts
after traversing both stings if cnt1>=cnt2 (in entire hash table) means that string2 is in string1....

- ashu July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use a hash table....
for each element it has 2 counts...cnt1 and cnt2
initialize both counts with 0
now traverse both strings and put it in hash table and maintain their respective counts
after traversing both stings if cnt1>=cnt2 (in entire hash table) means that string2 is in string1....

- ashu July 25, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More