Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

String reverse (String inString) {

Char[] ret= inString.toCharArray();
int length = inString.length;
for (int i = 0; i < length/2; i++){
char temp = ret [length-1-i];
ret [length-1-i] = ret [i];
ret [i] = temp;
}

return ret.toString();
}


Complexity: O(N)

- soconfusedgrad October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I will also do it in this way.

- Anonymous October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the length is odd, I think you have to add 1.

- Anonymous October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need not add 1.
If length is odd, the middle character is the same. No replacement needed.

- Anonymous November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Two pointer, one pointing to the top and one pointing the bottom. TopPtr moving downwards and BottomPtr moving upwards. Each time swap the char until TopPtr and BottomPtr meets. O(n/2)

- peter tang October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution, but the complexity is O(n). The "/2" part doesn't matter when n is in the order of millions.

- sethia November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a solution for C char. For std::string, the use constructor string output(input.rbegin(), input.rend());

- peter tang November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static String reverse (String st) {
String result = "";
int size = st.length();
for (int i = 1; i <= size; i++)
result += st.charAt( size - i);
return result;

}

- Tigran Hakobyan November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

A C# recursive function to reverse an input string
public string Reverse(string input)
{
if (input.Length == 1)
{
return input;
}
else
return string.Concat(input[input.Length-1], Reverse(input.Substring(0,input.Length-1)));
}

- bharat.chandra26 December 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think a follow up question to ask would be if there are any restrictions such as only reversing words etc. for example he may be asking to produce a result for a given input "what is your name" as "name your is what".

If this is the case I would split the string first into 2 dimensional array and then merge them back in reverse order.

- SHAD October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, but creating a linked list out of a string(char array) cannot be done in O(1). It takes O(n).. So your answer is not really relevant to the question asked.

- Anonymous October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry that comment was meant for the answer below.

- Anonymous October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There can be a better solution.
If you get to store the list in a doubly linked list, then you can reverse it in O(1).

You might understand what I said if you have studied Linked List from Thomas H Cormen's book. There is a problem there for reversing a linked list in O(1). The solution can apply here as well.

- sethia October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How would you return as a string?

Don't you have to use for loop to add each character in reversed linked list?

- Anonymous October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, but creating the linked list is a O(n) operation. So no benefit in creating a linked list here.

- Anonymous October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was talking about a special situation where you can store the string internally as a doubly linked list. This would be useful if you have to frequently reverse the string.

- sethia November 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Complexity O(n)


void main()
{
int len;
char *p,*ptr="Hello John!!!! How are yUo..";
len=strlen(ptr);

for(p=ptr+len;p>=ptr;p--)
printf("%c",*p);
}

- Kunal Bansal October 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char a[ ])
{
int s,e;
s=0;
e=strlen(a)-1;
while(s<e)
{
char t;
t=a[s];
a[s]=a[e];
a[e]=t;
++s;
--e;
}

- Nascent November 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* reverse(char * str){
if ( str == null){
return null;
}
char * end = str;
char temp;

while(end){
end++;
}
end--;
while(str < end){
temp = *end;
*end = *str;
*str = temp;
}
return str;
}

- xinen8721 November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

See through a mirror.
Complexity : simple.

- Anonymous November 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void ReverseString(char* str, unsigned int length)
{
    int left = 0;
    int right = length - 1;
    
    while( left < right )
    {
        str[left] ^= str[right];
        str[right] ^= str[left];
        str[left] ^= str[right];
        
        left++;
        right--;
    }
}

- Goose November 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void reverse(char arr[], int beg, int end) {
if(beg > end)
return;

reverse(arr, beg+1, end-1);

char t = arr[beg];
arr[beg] = arr[end];
arr[end] = t;

return;

}

- abhishek October 31, 2012 | 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