Amazon Interview Question for Software Engineer / Developers


Country: India




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

Please be more specific: are the elements that forms the word consecutive?

- zcb April 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brute force does not always mean NP-complete. In this case, a brute force algorithm would be O(N^2 + M^2 + M*N). This is definitely not an NP complete problem.

- Greg April 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution to this question can only be obtained through brute force. There isn't any polynomial time algorithm for this as far as I know. NP-Complete You just have to try all possible words from the matrix and check with the dictionary.

- npcomplete April 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

dude for which position are you applying at Amazon??..Was that telephonic interview or inperson?

- vineetsetia009 April 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my implementation to this question:

basicalgos.blogspot.com/2012/04/42-find-all-possible-words-from-2d.html

- Andy April 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the time complexity of ur soln? I guess it can be optimized largely.

- Nascent April 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone plz explain this more clearly "the valid words that can be formed using elements in a row, column and diagonal"?
e.g
C A E
T I N
B D K

what all words can be formed out of these?

- Learner April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It depends on the dictionary used. If we are using a traditional dictionary usage, valid words would be TIN and AID.
And these words should be formed horizontally (left to right), vertically (top to bottom) or diagonally.

- AB April 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer to an earlier (similar) questions on this site can be found at linearspacetime[dot] blogspot[dot]com

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

So heres the algorithm

First exemplify

Consider matrix

A. B. C.
D. E. F
G. H. I

Now simplify
//Make combination list
//Read row
// add each row to combination list
//Read column
// add each column to combination list
//Read diagonal
// add diagonal to combination list
// for each node in the combination list make permutations
//add each permutation to a hashtable as key and combination list node as value
// convert dictionary to a hash table
// for each permutation hash lookup hascode in dictionary
// if exists return true

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

This approach seems to be a brute force solution.
I guess combinational list contains all possible strings in a row/column diagonal.

Using hash table seems to be an overhead.
The dictionary may have been implemented as a hash table in the first place. Re creating the table is not a good idea.

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

I spent almost an hour to come up with below solution. could please give your feedback on solution.


Let us assume given matrix A[3][3] is

A N T
A I M
T O P

Also assume :: Is_word() check the string & print the string if it is a word

We need to check following strings start with A[0][0] (i.e A ) are words are not
Is_word ( "A[0][0]") ; // A[0][0] is A , first check, single letter could be a valid word
Case 1: Left to Right

AN , ANT

Case 2 : diagonal

AI , AIP

Case 3: Top to down
AA, AAT

similarly check following strings start with A[0][1] (i.e N ) are words are not

Is_word ( "A[0][1]") ; // A[0][1] is N ,first check, single letter could be a valid word
case 1: Left to right
NT
Case 2: diagonal
NM
case 3: top to down
NI , NIO

simlrly of other A[0][2], A[1][0] .... etc. location letters


Code for this problem is

Void Traverse_Matrix(char A[][], int n )
	{
		int i=0, j=0;

		char B[n];
		
		for (; i < n ; i++ )
		{
			for (; j< n ; j++ )
			{
				B[0] = A[i][j] ;
				Is_word(B, 1); // first check, single letter could be a valid word
				check_word(A,n,i,j+1,B,1,0); // left to right , last argument 0 specify , check left to right words
				check_word(A,n,i+1,j+1,B,1,1);// diagonal , last argument 1 specify , check diagonal words
				check_word(A,n,i+1,j,B,1,2);// left to right , last argument 0 specify , check all top to down words

			}
		}
	}

	void Check_word(char A[][], int n, int i, int j, char B[], int k, int direct )
	{
		if( i>= n || j>= n || k>=n )
		{
			return;
		}

		switch( direct )
		{
		case '0': B[k++] = A[i][j] ;
			      Is_word(B, k) ;
				  check_word(A,n,i,j+1,B,k,0); // left to right 
				  break;
		case '1': B[k++] = A[i][j] ;
			      Is_word(B, k) ;
				  check_word(A,n,i+1,j+1,B,k,1);// diagonal
				  break;
		case '2': B[k++] = A[i][j] ;
			      Is_word(B, k) ;
				  check_word(A,n,i+1,j,B,k,2);// top to down 
				  			  
		}
	}

Time complexity O(n^3) .


Please let me know if find any mistakes in my approach.

- Siva.Sai.2021 May 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks correct

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

You need to consider left diagonal.
Also reverse the strings and check them too.

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

You need to consider left diagonal.
Also reverse the strings and check them too.

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

I doubt that reverse string and permutations have to be checked for! If we could do all of that, we might just pick up random words too....Anyway, this is insignificant as the objective of the question have been met.

Regarding complexity,I do not think it iO(n^3).


The code excutes row*column*offset

where row and column are basically m,n. Offset is the range of indexes you are considering - starting from 1.

since row*columns = n, the complexity comes out to be O(Offset*n) i.e O(n).

Please correct me if I am wrong!!!

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

I doubt that reverse string and permutations have to be checked for! If we could do all of that, we might just pick up random words too....Anyway, this is insignificant as the objective of the question have been met.

Regarding complexity,I do not think it iO(n^3).


The code excutes row*column*offset

where row and column are basically m,n. Offset is the range of indexes you are considering - starting from 1.

since row*columns = n, the complexity comes out to be O(Offset*n) i.e O(n).

Please correct me if I am wrong!!!

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

I doubt that reverse string and permutations have to be checked for! If we could do all of that, we might just pick up random words too....Anyway, this is insignificant as the objective of the question have been met.

Regarding complexity,I do not think it iO(n^3).


The code excutes row*column*offset

where row and column are basically m,n. Offset is the range of indexes you are considering - starting from 1.

since row*columns = n, the complexity comes out to be O(Offset*n) i.e O(n).

Please correct me if I am wrong!!!

- Ashish May 06, 2012 | Flag


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