Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

What is the output for below string
i/p==abcd
o/p==adbc

- kunapareddy.sunil May 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think ans is 4..
b shifted by 1
c shifted by 1
d shifted by 2.
If this is correct ans, then it can be done in O(n^2) time .. if we take an array of 26, then it can be done in one scan O(n).

- bharat May 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If only the number of moves is asked,
operations = number of chars in wrong position precisely.

- theGhost September 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Though questions does not seems clear to me. I assume that you want to convert one string to another by rotating it.

In this case append first string with itself and find index of start of the second string in it. That is the no of shifts required.

- Nitin Gupta May 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For the above example, we will scan both the strings and as soon as we encounter a non-matching character(in above example b and d). we'll store these two characters. Now in string 1(i/p string) we'll find the position of b and d and subtract them to get the number of required shifts. So number of shifts will be 2.
Hence the answer : 2.

Please correct me if the solution fails for any case.

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

Then what about C

- kunapareddy.sunil May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If u will shift b automatically c will get shifted..

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

Code in c#:
ENJOY THE CONCEPTTTTTTTTTTTT

.......

static void Main(string[] args)
{
string s = "ABCDEFGH";
string newS = ShiftString(s);
Console.WriteLine(newS);
}
public static string ShiftString(string t)
{
char[] c = t.ToCharArray();
char save = c[0];
for (int i = 0; i < c.Length; i++)
{
if (c[i] != c[0])
c[i] = c[i - 1];
}
Console.WriteLine(c);
String s = new string(c);
return s;
}

- oTHHAH September 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

int main(int argc, char** argv)
{
	if (argc != 3)
	{
		Usage();
	}
	else
	{
		char* s1 = argv[1];
		char* s2 = argv[2];

		if (strlen(s1) != strlen(s2))
		{
			printf("the two string don't have the same size : \n %s -> %d \n %s -> %d", s1, strlen(s1) , s2, strlen(s2));
		}
		else
		{
			int i = 0;
			int shift = 0;
			while(i < strlen(s1) )
			{
				int isexisting = find(i, s1, s2);

				if (isexisting != -1)
				{
					s2[isexisting] = ' ';// replace a used charcter in a string by a space in order to don't reuse it

					if (isexisting != i)
					{
						shift++;
					}
				}
				else
				{
					printf("2 strings don't match");
					return 0;
				}

				i++;
			}
			printf("shift number: %d\n", shift/2);
		}
	}
	return 0;
}

void Usage()
{
	printf("\n ./NeededShift \"string1\" \"string2\" \n");
}

//find s1[pos] in s2 return -1 if not found 
int find(int pos, char* s1, char* s2)
{
	int i = 0;

	while(i < strlen(s2))
	{
		if (s1[pos] == s2[i])
		{
			return i;
		}
		i++;
	}
	return -1;
}

- Mohamed May 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Levenshtein Distance algorithm can be applied to find the distance between two strings.

- Naveen May 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If space is not an issues, then
1.Scan the original string and store the positions as values in the hash.
2.Now scan the next string and for each char, get the absolute difference of disntace of
the first string char from hast table and the current position.
3.Sum all distances.

- Anonymous June 23, 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