Amazon Interview Question for Software Engineer / Developers

• 1
of 1 vote

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

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

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).

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.

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.

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.

Please correct me if the solution fails for any case.

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

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

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

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

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;
}

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");
}

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;
}``````

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.

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.

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.

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.

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.

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.