Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

The question isn't exactly clear, what is forward, which diagonal and whether we need to find the number of occurrences of a given word(s) or the occurrences of all possible words.

Anyway, I'll assume the following:
1. We're counting the number of occurrences of a given word.
2. Forward means right.
3. Diagonally means right and down.

My approach is to use dynamic programming and basically count the number of occurrences of every suffix beginning from every array element. This solution (and there probably are better ones) has O(k*n^2) run-time and space complexity (k is the length of the input string).

private static int[][][] buildDPMatrix(char[][] arr, char[] str){
		if ((arr==null) || (str==null)){return null;}
		for (int i=0;i<arr.length;i++){
			if (arr.length != arr[i].length){return null;}
		}
		
		int[][][] dp = new int[arr.length+1][arr.length+1][str.length];
		for (int i=0;i<dp.length;i++){
			for (int j=0;j<dp[i].length;j++){
				for (int k=0;k<dp[i][j].length;k++){dp[i][j][k]=0;}
			}
		}
		for (int k=str.length-1;k>=0;k--){
			for (int i=0;i<dp.length-1;i++){
				for (int j=0;j<dp.length-1;j++){
					dp[i][j][k] = (arr[i][j]==str[k]) ? 
							((k==str.length-1) ? 1 : dp[i+1][j][k+1] + dp[i][j+1][k+1] + dp[i+1][j+1][k+1]) : 0;
				}
			}
		}
		
		return dp;
	}
	
	public static int findNumOfOccurrences(char[][] arr, String str){
		if ((arr==null) || (str==null) || (str.length()==0)){return 0;}
		int[][][] dp = buildDPMatrix(arr, str.toCharArray());
		int count = 0;
		if (dp!=null){
			for (int i=0;i<dp.length-1;i++){
				for (int j=0;j<dp.length-1;j++){count+=dp[i][j][0];}
			}
		}
		
		return count;
	}

- EulGam05 December 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not really sure where you are going with this, but it appears to be a very brute force. While DP does not need to be recursive, you still appear to check every character for every slot in the NxN character matrix.

- nothing special here December 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

forward means right (x+1,y)
down mean (x,y+1)
diagonal means (x+1,y+1)

it can be done with BFS. {search the no. of occurance of a given word example "sachin" in the whole NxN matrix}

w | s | r | t | g | g|
a | a | c | h | i | n |
k | c | h | u | j | j |
o | h | i | n | y | q |

in this sachin can be found out 3 times.

- GOOGLE_SDE December 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't understand your restrictions on directions. What's "forward"? Do you mean you can go to the right column? And which "diagonal" are you talking about? All four?

Anyways, I would imagine you could do a modified DFS.

- nothing special here December 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the word is given, a brute force solution is check each point(i,j) in the matrix, and if m[i][j]=word[0],then check three directions with length equals word.length(). this is a O(mnk) complexity, where m,n is matrix size, k is word size.

if the word finding process will execute multi times,then maybe generate all prefix of rows,cols,dialogues of the matrix, and use trie is a better idea.

- busycai December 16, 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