Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

@Shashi... Well I didn't got why U are doing the last if check
if(S[i]=='?'){
S[i]='A';
}

This makes logic highly vulnerable..
Example..:-
You are always replacing last character with alphabet 'A' if its '?' why ??

- hprem991 January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Processing from the end of a string is not always a solution, consider following example:
S: AKTAK??????
p: AKTAK

if we start for the end of the string, than result will contain only 2 substrings: AKTAKAAKTAK
but if we'll move forward, we'll have 3 substrings: AKTAKTAKTAK

- Perfect.Hash January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There won't be any problem even if we process from the end. I processed from the end to make the final string lexicographicaly sorted .Previously i didn't consider about overlaping substring. I have modified the code to take care of it.

- Shashi January 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Working Code ....

{
string fillString(string s, string w){
    int m,i;
    m=s.size()-1;
    i = w.size()-1;
    bool flag = false;
    int start,cur;
    while(m>-1){
        
        if(s[m]==w[i] || s[m]=='?'){
            start = flag ? start : m;
            i--;
            flag = true;
        }
        else{
            m = flag ? start : m;
            flag=false;
            i = w.size()-1;
        }
        if(i==-1 && flag == true){
            cur=m; i=0;
            while(cur<=start){
                s[cur++]=w[i++];
            }
            flag = false;
            i = w.size()-1;
        }
        m--;
    }
    cur=s.size()-1;
    while(cur>-1){
        if(s[cur]=='?')s[cur]='A';
        cur--;
    }
    return s;
}

}

i think the logic is same as of shashi. time complexity min O(n) max O(n*m)

- Crazy Tesla January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can you please explain why it has to be end of string? cant we do the same from start of string?

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

shashi:
can u please explain the logic

- ritesh bajaj January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we write same code for java it throws StringIndexOutOfBounds so in the if statement you just have to check if k>=0 so that error doesnt appear.

- Anonymous January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how it works.
1. Get the length of String s and p
2. start comparing from the end of String s and String p if you find two chars are equal or ? then keep comparing
3. if the whole string p is in S then replace all those ? with the characters of p.
4. If p is not part of S then just add the first character of p for that character and decrements the index by i--

- Anonymous January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why does it have to be end of string? why not from start of string?

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

How about this?

class CCID15259735 {
	// ST [11:21] ET [11:30]
	public static void q1(){
		String S = "ABAAMA????MAZON????????";
		String P = "AMAZON";
		
		// String S ="?????";
		// String P ="ABCD";
		
		for(int i=0; i< S.length();i++){
			int k = i;
			char thisChar;
			boolean isPinS = true;
			for(int posInP = 0; posInP < P.length() && k < S.length(); posInP++){
				thisChar = S.charAt(k);
				if(thisChar == '?' || thisChar == P.charAt(posInP)){
					k++;
					continue;
				}
				isPinS = false;
				break;
			}
			
			if(isPinS && (k-i) == P.length()){
				System.out.println("Fill [i][" + i + "] till [k][" + k + "] of S = " + S.substring(i, k));
				i = k; // Once matched then progress i to k.
			}
		}
	}
}

- shrek.apple January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea: may be we can apply kmp with a small modification?

- Vincent January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

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

using namespace std;

int main() {
    char s[] = "ABAAMA????MAZON????????";
    char t[] = "AMAZON";

    int j = 0;
    for (int i = 0; i < strlen(s); i++) {
        if (s[i] == t[j]) {
            j++;
            j = j % strlen(t);
        }
        if (s[i] == '?') {
            s[i] = t[j++];
            j = j % strlen(t);
        }
    }

    cout << s;
    
    /* It Prints -- "ABAAMAZONAMAZONAMAZONAM"
     */

}

- isandesh7 January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think following algorithm should be applied:
When we find several ? symbols in the middle of the string, we should calculate
a) How much symbols should be appended to the left of ???? to complement p string.
b) How much symbols should be appended to the right of ???? to complement p string.
if a<b - append symbols to the left.

For example:
S: TERR????RRA
p: TERRA

step #1: TERRA???RRA
step #2: TERRA?TERRA

Repeat algorithm till we have any ? symbol in the S string.

- Perfect.Hash January 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How is the string "ABAAMAZONAMAZONAAAMAZON" lexicographically sorted? COuld someone please explain the question to me?

- Second Attempt February 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. Look if u can fit AMAZON in the string
2. If u r not able to fit AMAZON , then fill those ? with A, so that these sequence of ? which are not able to accomodate AMAZON are atleast lexicographically sorted

hope that makes sense

- PM May 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static char[] fillString(char[] str1, char[] str2){
int flag1 = 0;
int flag2 = 0;
for(int i=0;i<str1.length;i++){
System.out.println(str1[i]);
if(flag1>0){ // check the next in series
if(str1[i]=='?'){
if(flag1==str2.length)
flag1=0;
str1[i]= str2[flag1];
flag1++;
System.out.println("===="+flag1);
continue;
}
if(str1[i]==str2[flag1]){
flag1++;
System.out.println("===="+flag1);
continue;
}
}
if(str1[i]==str2[0]){ // check the first character
flag1=1;
System.out.println("===="+flag1);
continue;
}
if(str1[i]=='?'){ // if no match in string is present before
if(flag2==str2.length)
flag2=0;
str1[i]=str2[flag2];
System.out.println("+++++"+flag2);
flag2++;
continue;
}
}
return str1;
}

// Str1 = "ABAAMA?????MAZON??????????AMA??????????????????"
// Str2 = "AMAZON"

want to have your precious feedback, about the complexity and otherthings .. Please review it and let me know

- vrajendra.singh.mandloi March 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

work with both examples
{{
static String fillString(String S, String p){
StringBuilder SB = new StringBuilder(S);
int skip = p.length()-1;
boolean keep = false;
for (int i = SB.length()-1; i >= 0; i--){
if (SB.charAt(i) == '?'){
SB.setCharAt(i, p.charAt(skip));
skip--;
if (skip < 0){
if (keep) skip = p.length()-1;
else skip = 0;
}
}
else {
if (!keep) {
skip = p.length()-1;
keep = true;
}
if (SB.charAt(i) == p.charAt(skip)){
skip--;
if (skip < 0) skip = p.length()-1;
}
else {
skip = p.length()-1;
keep = false;
}
}
}
return SB.toString();
}}

- pckz November 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

works with both examples:

static String fillString(String S, String p){
		StringBuilder SB = new StringBuilder(S);
		int skip = p.length()-1;
		boolean keep = false;
		for (int i = SB.length()-1; i >= 0; i--){
			if (SB.charAt(i) == '?'){
				SB.setCharAt(i, p.charAt(skip));
				skip--;
				if (skip < 0){
					if (keep) skip = p.length()-1;
					else skip = 0;
				}
			}
			else {
				if (!keep) {
					skip = p.length()-1;
					keep = true;
				}
				if (SB.charAt(i) == p.charAt(skip)){
					skip--;
					if (skip < 0) skip = p.length()-1;
				}
				else { 
					skip = p.length()-1;
					keep = false;
				}
			}
		}
		return SB.toString();

// Sorry for duplicated answer

- pckz November 26, 2014 | 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