Amazon Interview Question for Software Engineer in Tests


Country: United States




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

I have some simple string reversal code that uses recursion, sometimes they like to see that kind of stuff.

public String reverseString(String s) {
        if (s.length() <= 1) {
            return s;
        } 
        return reverseString(s.substring(1,s.length())) + s.charAt(0);
    }

It is fairly straigh forward

- M.Zimmerman6 December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Let the input string be “i like this program very much”
Algorithm:
1) Reverse the individual words, we get the below string.
"i ekil siht margorp yrev hcum"
2) Reverse the whole string from start to end and you get the desired output.
"much very program this like i"
check this link for more detail : www(dot)geeksforgeeks(dot)org/archives/7150

- ratn December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

string word = "I like this world";
string[] words = word.Split(' ');
for (int i = words.Length - 1; i >= 0; i--)
{
Console.WriteLine(words[i]);
}
Console.WriteLine("**********");

for (int i = 0; i < words.Length; i++)
{
char[] chars = words[i].ToCharArray();
for (int j = chars.Length - 1; j >= 0; j--)
{
Console.Write(chars[j]);
}
Console.WriteLine();

}

Console.ReadLine();

out:
world
this
like
I
********
I
ekil
siht
dlrow

- vitaly radionov December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Using a stack can also reverse the string.
1. Push all the chars in the Stack
2. While popping the chars from Stack and storing it in another char array

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

import java.util.Hashtable;
import java.io.Console;
import java.util.*;
import java.io.*;
import java.lang.*;

public class Reverse {
public static void main(String[] args) {
String str1;

Console console = System.console();
str1 = console.readLine("Enter the string: ");

//reverse the whole string
String s=reverseString(str1);

//split the string and reverse all the substrings
String[] strArr = s.split(" ");

// StringBuilder sb = new StringBuilder();
String result = new String();
for(String part:strArr)
{

result += reverseString(part) + " ";
}


System.out.println(result);

}
// 12345 6789
// 9876 54321
// 6789 12345


private static String reverseString(String str)
{
char[] ch = str.toCharArray();

int i=0;
int j = ch.length-1;

char c;
while (i<j)
{
c = ch[j];
ch[j] = ch[i];
ch[i] = c;

i++;
j--;
}

return new String(ch);
}
}

- Lyubomir December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//program to print the string in the reverse order but not the words present in it
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void reverse(char * arrayin);
void rev_words(char * arrayin);
int main()
{
     printf("Enter the string\n");
     char input[100];
     gets(input);
     reverse(input);
     rev_words(input);
     printf("The output is:\n");
     puts(input); 
     return 0;
}
void reverse(char * arrayin)
{
     int i;
     int length;
     char temp;
     length = strlen(arrayin);
     for(i=0;i<(length/2);i++)
     {
          temp = *(arrayin+i);
          *(arrayin+i) = *(arrayin+length-1-i);
          *(arrayin+length-1-i) = temp;
     }
}
void rev_words(char * arrayin)
{
	int i,length, count,lengthx;
	int start, end,m,j;
	char temp;
	count = 0;
	j = 0;
	length = strlen(arrayin);
	for(i=0; i<length; i++)
	{
		if(arrayin[i]==' ')
		{
			count = count + 1;	
		}
	}
	start = 0;
	end = 0;
	for(i=0;i<length;i++)
	{
		j = 0;
		if(arrayin[i]==' ' || i == (length-1))
		{	
			if(i!= (length-1))
			{
				end = i - 1;
			}
			else if(i == (length-1))
			{
				end = i;
			}
			lengthx = end - start + 1;
			for(m=start; m <= (start - 1 + lengthx/2); m++,j++)
			{
				temp = arrayin[m];
				arrayin[m] = arrayin[end - j];
				arrayin[end - j] = temp;
			}
			start = i + 1;
		}	
	}
}

- vishal.cs.bits December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using C++ and STL

#include <string>
#include <sstream>

int main(int argc, const char * argv[])
{
    string s("Somewhere  \n down the road");
    //For replace stringstream by fstream for files 
    istringstream iss(s);
    ostringstream oss ("");
    
    string sub;
    do
    {

        iss >> sub; // all delims might be lost - check getline syntax to keep line integrity
        streamsize pos = oss.tellp();
        oss.str( sub + " " + oss.str() ); // prepend substring, separator
        oss.seekp( pos + sub.length() );
        cout << "Substring: " << sub << endl;
    } while (!iss.eof());
    
    //oss.seekp(0);
	cout <<"Reversed String: "<< oss.str();
    
    return 0;
}

- Aztec warrior December 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

here is the output, I think the seekp tellp isnt needed I just copy pasted from insert code (in case you dont want to reverse) ;)

Substring: Somewhere
Substring: down
Substring: the
Substring: road
Reversed String: road the down Somewhere

- Aztec warrior December 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;


void ReverseString (char word[], int start, int end) {
  char tmp;
  while (end > start) {
	tmp = word[start];
	word[start++] = word[end];
	word[end--] = tmp;
  }
}

void ReverseWord (char word[]) {
  int start = 0, end = 0, length = strlen (word);
  ReverseString (word, start, length-1);
  
  while (end < length) {
	if (word[end] != ' ') {
	  start = end;
	  while ((end < length) && (word[end] != ' ')) {
		end++;
	  }
	  end--;
	  ReverseString(word, start, end);
	}
	end++;
  }
}

int main () {
  char word[] = "HELLO WORLD. THIS IS FIRST SUCCESSFUL PROGRAM.";
  ReverseWord(word);

  cout << word;

  getch();
  return 0;

}

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

#include<iostream>
#include<stdio.h>
using namespace std;
int xlen(char *s)
{
	int length=0;
	while(*s!='\0')
	{
		length++;
		s++;
	}
	return length;
}
int main()
{
	char a[100];
	int i;
	gets(a);
	int len=xlen(a);
	for(i=len;i>=0;i--)
	{
		cout<<a[i];
	}
	return 0;

}

- Junaid April 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import java.util.*;
public class StringReverse{
public static void main(String[] args) {
String st1= "This is a new string and want it to be reversed";
char[] ch1= st1.toCharArray();
int length =ch1.length-1;
for(int i=length;i>=0;i--){
System.out.print(ch1[i]);
}}}

- Anil January 06, 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