Amazon Interview Question for SDE-2s


Country: United States




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

void findCount_iter(const std::string& str, int currIndex, char currChar, int& count)
{
    if (str.size() == currIndex)
    {
        // Only if we have a valid string, increment the count
        if (currChar == 'c')
        {
            ++count;
        }
        
        return;
    }

    // Possibly including the current char
    if (str[currIndex] == currChar)
    {
        findCount_iter(str, currIndex + 1, currChar, count);
    }
    else if (str[currIndex] == currChar + 1)
    {
        findCount_iter(str, currIndex + 1, currChar + 1, count);
    }

    // Skipping the current char
    findCount_iter(str, currIndex + 1, currChar, count);
}

int findCount(const std::string& str)
{
    int count = 0;
    findCount_iter(str, 0, 'a' - 1, count);
    return count;
}

- Surya February 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void findCount_iter(const std::string& str, int currIndex, char currChar, int& count)
{
    if (str.size() == currIndex)
    {
        // Only if we have a valid string, increment the count
        if (currChar == 'c')
        {
            ++count;
        }
        
        return;
    }

    // Possibly including the current char
    if (str[currIndex] == currChar)
    {
        findCount_iter(str, currIndex + 1, currChar, count);
    }
    else if (str[currIndex] == currChar + 1)
    {
        findCount_iter(str, currIndex + 1, currChar + 1, count);
    }

    // Skipping the current char
    findCount_iter(str, currIndex + 1, currChar, count);
}

int findCount(const std::string& str)
{
    int count = 0;
    findCount_iter(str, 0, 'a' - 1, count);
    return count;
}

- Surya February 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int CountSequences(string input)
        {
            char[] chars = input.ToCharArray();
            int count = 0;
            CountSequences(chars, 0, 'a', ref count);
            return count;
        }
        //abcabc
        private static void CountSequences(char[] chars, int startIndex, char lookupChar, ref int count)
        {
            if (lookupChar > 'c') return;
            for (int currentIndex = startIndex; currentIndex < chars.Length; currentIndex++)
            {
                if (chars[currentIndex] == lookupChar) //found a match
                {
                    if (lookupChar == 'c')
                    {
                        count++;
                    }
                    else
                    {
                        CountSequences(chars, currentIndex + 1, (char)(lookupChar + 1), ref count);
                    }
                    CountSequences(chars, currentIndex + 1, lookupChar, ref count);
                }
            }
        }

- IC February 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

The O(n) idea is to use dynamic programming. If letter is 'a' and you know the number of valid strings ending in 'a' for characters up to position i, the new number is 2n+1 because you keep all strings up to position i (n), you add this 'a' to all previous strings (+n) and you can have this char as a separate string (+1).
With a similar thinking
nBs = 2 nBs + nAs
nCs = 2 nCs + nBs

E.g.
a b c a b c
1 1 1 3 3 3 nAs
0 1 1 1 5 5 nBs
0 0 1 1 1 7 nCs

- Stefan Manea February 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can be done by recursive or iterative way.
Recursive way is very inefficient as it can be of the order of O(n2) plus a lot of method stacks gets created.

Iterative solution is best solution that gets over in O(n) times. Its given as below.
Its some one wants to know more about how its calculated then he can email me at teji.catia@gmail.com:

public class IterativeSeq {
	public static void main(String[] args){
		IterativeSeq t= new IterativeSeq();
		System.out.println("output="+t.CountSeq("abcabcaabbcc"));
	}	
	public static int CountSeq(String input){	
		int acount=0, bcount=0, ccount=0;
		char[] chars = input.toCharArray();
		for(int i=chars.length-1; i>=0;i--){
			if(chars[i]=='c'){
				ccount++;
			}else if(chars[i]=='b'){
        		bcount=((int)Math.pow(2,ccount)-1)+(bcount*2);   		
        	}else if(chars[i]=='a'){
        		acount=bcount+(2*acount);
        	}
		}
		return acount;
	}

}

- Anonymous March 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can be resolved in either recursive or iterative way.
Recursive way is very inefficient as it may take O(n^2) or more and causes many method stacks being built upon.
Iterative solution is best with guaranteed O(n) running time. Its a below.
If someone wants to know about how formula is derived. He can mail me at teji.catia@gmail.com.

public class IterativeSeq {
	public static void main(String[] args){
		IterativeSeq t= new IterativeSeq();
		System.out.println("output="+t.CountSeq("abcabcaabbcc"));
	}	
	public static int CountSeq(String input){	
		int acount=0, bcount=0, ccount=0;
		char[] chars = input.toCharArray();
		for(int i=chars.length-1; i>=0;i--){
			if(chars[i]=='c'){
				ccount++;
			}else if(chars[i]=='b'){
        		bcount=((int)Math.pow(2,ccount)-1)+(bcount*2);   		
        	}else if(chars[i]=='a'){
        		acount=bcount+(2*acount);
        	}
		}
		return acount;
	}

}

- teji.catia March 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There can be more answer to the provided example so output should be more than 7.
i.e. {1,4,5,3,6};{1,4,2,3,6};{1,4,2,3,6};{1,5,3}; etc according to the question. let me know i am wrong.

- Pushkal September 02, 2018 | 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