Amazon Interview Question for Software Engineer / Developers


Team: Prime
Country: United States
Interview Type: Phone Interview




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

simple kmp

- ssikka25 February 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you know that KMP is actually equivalent to the NFA/DFA approach?

- IBS February 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could be easily done with a suffix tree.

- Guy February 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>
#include<string.h>

int occur(char[],char[]);

int occur(char sent[],char pattern[])
{

	int count=0;

	for(int i=0,j=i;sent[i]!='\0';)
	{
		if(sent[i+j]==pattern[j]&&pattern[j]!='\0')
		{
			j++;
			
			
		}
	   else if(j==strlen(pattern))
		{
			count++;
			i=i+j;
			j=0;
		}
		else
		{
			i++;
			j=0;
			
		}
		
	}
	return count;
}

int main()
{
	char sent[] = "aabaabaaabbbabababddfggaabbbasab";
	char pattern[] = "aba";
	int result = occur(sent,pattern);
	printf("\nNo of Occurrences --- > %d",result);
	return 0;
}

- Monis Majeed February 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void countOccurence(String str, String pattern)
    {
    int n = 0, i = 0;
    while (i < str.length()
            && (i = str.indexOf(pattern, i)) != -1) {
        ++n;
        ++i;
    }
    System.out.println("n: " + n);
   }

It should be O(n) time complexity I think.

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

public class FindPatternOccurrences {

	public static void main(String[] args) {
		String str = "aabababcfdc";
		String pattern = "ab";
		matchPattern(str, pattern);
	}

	public static void matchPattern(String str, String pattern) {

		int count = 0;
		for (int i = 0; i < str.length() - 1; i++) {
			String str1 = str.substring(i, i + 2);
			if (str1.equals(pattern))
				count++;
		}
		System.out.println("NUmber of occurrences= " + count);
	}

}

- Ravi Kumar February 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This prints the number of occurrences of the pattern:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class SubString {
public static void main(String[] args) {
	String str= "abbbacctlkababcdjlkcccabcdaaabbbabcd";
	String temp = "abcd";
	Map<String,Integer> map = new HashMap<>();
	int len = str.length();
	System.out.println(len);
	int templen = temp.length();
	for(int i=0;i<len-templen+1;i++){
		if(map.containsKey(str.substring(i,i+templen)))
		map.put(str.substring(i,i+templen), map.get(str.substring(i,i+templen))+1);
		else
			map.put(str.substring(i,i+templen), 1);	
	}
	System.out.println(map.get(temp));
	
	
}
}

- Ravi February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

you are inserting every substring into the hasmap and only getting the desired string count in the end, wasting a lot of memory, since you're only interested on the target string why don't you do just count it's occurrences like this:

public static void main(String[] args) {
	String str= "abbbacctlkababcdjlkcccabcdaaabbbabcd";
	String temp = "abcd";	
	int len = str.length();
	int count = 0;
	int templen = temp.length();
	for(int i=0;i<len-templen+1;i++){
		String tempStr = str.substring(i,i+templen));
		if (tempStr.equals(temp)) {
			count ++;
		}
	}
	System.out.println(count);
}

- guilhebl February 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yeah.. this one's better... Thanks!

- Ravi February 06, 2014 | 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