Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

We can use trie to solve this in O(n).

construct trie taking three letter(consecutive) words.
Now after finishing trie, again go thourgh the array in the reverse order and if we find the word in trie print that string.

- krishna May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Here is the code based on the Trie, the code is in java and I am not right now posting the Trie class, you can use any standard Trie implementation available on the net

public void stringProblem (String str)
	{
		Trie tr = new Trie();
		if(str!= null)
		{
			boolean result = false; 
			int len = str.length();
			for(int i = 0; i< len-2;i++)
			{
				String subStr = str.substring(i, i+3);
				tr.insertWord(subStr);
			}
			
			for(int i = len-1;i>1;i--)
			{
				String subStr = str.substring(i-2, i+1);
				StringBuffer sB = new StringBuffer(subStr).reverse();
				if(tr.searchWord(sB.toString()))
				{
					result = true;
					break;
				}
			}
			
			if(result)
				System.out.println("Match Found");
			else
				System.out.println("Match Not Found");

		}
		
	}

/* TestCases
					s.stringProblem("abcdefhhiufedss");
					s.stringProblem("abcdefhhiufdss");
					s.stringProblem("aaa");
					s.stringProblem("aa");
					s.stringProblem("");
					s.stringProblem("ssssssssssssssss");
*/

- Ashupriya June 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

By using Rabin-Karp algorithm find the hash value for all the 3 letter strings
ex: ABCDEFDCBL

find hash value for ABC - hash it
find hash value for BCD - hash it
find hash value for CDE - hash it
.
.
find hash value for DCB - hash it
find hash value for CBL - hash it
starting from end of the string again find the hash values for all 3 leter strings and check whether the hash is already present in hash table

find hash value for LBC - check if this hash is already present in hash table 1
find hash value for BCD - check if this hash is already present in hash table 1
find hash value for CDF - check if this hash is already present in hash table 1
.
.
find hash value for CBA - check if this hash is already present in hash table 1

time complexicity O(n)
space complexicity O(n)

- Anonymous May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how is it O(n) ??

its O(mn), where m =3.

- Himz May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you don't need to calculate hash every time for all 3 / K letter strings...
In the given example ABCDEFDCBL, first calculate hash for first 3 letters ABC, then you can calculate hash for BCD like below

hash(ABC) - A + D, this is what Rabin-Karp algorithm...

so this is O(n) time complexicity

- Anonymous May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We can use a bitset of size (26*26*26 - all combinations of 3 letter string). Now parse the string by taking 3 letters at a time and calculate the bit number to be set. As string is abcde; first take abc = (0*26^2 + 1*26^1 + 3*26^0), basically get the value on base 26. set the bit and check the reverse sting also "cba" if this is set, we get the string.

- Akash Jain May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hash is cheaper

__int32 GetHash( __int8* pattern, bool direction )
{
  __int8 b1 = direction ? *pattern++ : *(pattern-2);
  __int8 b2 = direction ? *pattern++ : *(pattern-1);
  __int8 b3 = *pattern;
  __int32 result = __int32(b1 << 24) + __int32(b2 << 16) + __int32(b3 << 8);

  return result;
}

- LBaralgeen May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool find(const char* input,  int len, char out[])
{
  std::set<std::string>> threeletterwords;
  for (int i = 0; i < len; ++i)
  {
     char temp[4] = { input[i], input[i+1], input[i+2], 0};
     char revtemp[4] = { input[i+2], input[i+1], input[i], 0};
     
     if (threeletterwords.find(revtemp) != threeletterwords.end())
     {
         strcpy(out, temp);
         return true;
     }
     
     
     threeletterwords.insert(temp);
   
  }
  
  return false;
}

Hashtable can be used instead of set.

- bugbug May 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one seems to be a brute force method, which takes O(n2) complexity.

- krishna May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its worst time complexity is O(nlogn). As set is a BST in STL, and cost of searching in BST is logn

- bugbug May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

also consider option of using hashtable instead of BST.

- Anonymous May 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// without DS

bool CheckPolinoms( char* forward, char* reverse )
{
  if( 0x00 == forward || 0x00 == *forward || 0x00 == reverse || 0x00 == *reverse )
  {
    return false;
  }
  bool f = ( *forward++ == *(reverse  ) );
  bool m = ( *forward++ == *(reverse-1) );
  bool l = ( *forward++ == *(reverse-2) );
  return f && m && l;
}

void test17()
{
    char  test[] = "abcdefdcbnp";
    for( size_t i = 0; _countof(test)-2 > i; i++ )
    {
        for( size_t j = _countof(test)-2; 2 < j; j-- )
        {
           if( CheckPolinoms(  &test[i], &test[j] )  )
           {
               cout << i << ", " << j << "\n";
               break;
           }
        }
    }
}

- LBaralgeen May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use suffix tree to get O(n) solution

- allFail May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suffix Tree is the right answer.

- Anonymous May 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suffix Tree or Suffix Array is the right answer.

- Anonymous May 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No need of Subfix, just do it in O(n) within one loop :

public static void main(String[] args) 
	{
		String s = "xyzabcddsacba";
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		for(int x = 0; x < (s.length() - 2) ; x++)
		{
			String value = s.charAt(x)+ "" + s.charAt(x + 1) +""+ s.charAt(x + 2) + "";
			String reverse = s.charAt(x + 2)+ "" + s.charAt(x + 1) +""+ s.charAt(x) + "";
						
			if(map.get(value) != null)
			{
				System.out.println(" Result : "+reverse);
				break;
			}
			else{
				map.put(reverse, reverse);
			}
		}		
	}

- Obaid August 13, 2013 | 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