Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

package com.algorithm.amazon;

import java.util.StringTokenizer;

public class ReverseSentence {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(reverseWordsInSentence("I am Don"));
	}

	private static String reverseWordsInSentence(String string) {
		// TODO Auto-generated method stub
		StringTokenizer st=new StringTokenizer(string," ");
		String revString="";
		while(st.hasMoreTokens())
		{
			revString=st.nextToken()+" "+revString;
					
		}
		return revString;
	}


}

- Vir Pratap Uttam April 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Reverse complete string and then reverse each words from starting.
Eg: I am Don

Step 1: noD ma I
Step 2: Don am I

Please suggest me better solution if exist.

- Razz December 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

If additional buffer is allowed - stack can help.

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

A little python code the works. Complexity is O(n).

def f(x): return x+" "
def f(x): return x+" "

def reverse(toReverse):
answer=toReverse.split()
answer.reverse()
answer="".join(map(f,answer))
return answer

if __name__ == '__main__':
print (reverse("I am trying to learn python"))

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

a straight forward Java Solution:

public String reverse(String string) {
		String stAry[] = string.split(" ");
		StringBuilder b = new StringBuilder();
		for (int i = stAry.length - 1; i >= 0; i--) {
			b.append(stAry[i]).append(" ");
		}
		return b.toString().substring(0, b.lastIndexOf(" "));
	}

- Prithvi December 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why does not your code return this:
return b.toString();

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

I see, to eliminate the extra space in the end, you can do this:

for (int i = stAry.length - 1; i > 0; i--) {
			b.append(stAry[i]).append(" ");
		}
		b.append(stAry[i]);
		return b.toString();

- Anonymous December 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<algorithm>
#include<string>
#include<iostream>

using namespace std;

int main()
{

string inpStr,inpWrd;
int age;

cout << "Enter the string :";

getline(cin,inpStr);

cout<<"Input String is : "<<inpStr<<endl;

string delimiters = " ";
size_t current;
size_t next = -1;
do
{
  current = next + 1;
  next = inpStr.find_first_of( delimiters, current );
  inpWrd=inpStr.substr( current, next - current ) ;
  reverse(inpWrd.begin(),inpWrd.end());

  cout<<inpWrd<<" ";
}
while (next != string::npos);

cout<<endl;
return(0);
}

Complexity :
Time : O(n)

- callmeadi December 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Input: I am Don
Output: Don am I

Algorithm:
- Start on the input string from the reverse side.
- If encounter any character other than space, put that in a stack.
- If encountered a space, take all the characters out of the stack one by one and add them to the output string, append the space, and continue.
- As you are traversing in reverse, you'll encounter the word letters in reverse. Pushing them in stack and popping them out will switch them to make a forward word.

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

Recursively

public static string ReverseStringInOrder(string x)
        {
            char[] a = x.ToCharArray();
            char[] b = new char[a.Length];
            ReverseWordInOrder(a, a.Length-1, b);
            return new string(b);
        }

        public static void ReverseWordInOrder(char[] a, int s, char[] res)
        {
            if (s < 0)
                return;

            int sp = -1;
            for (int i = s; i >= 0; i--)
            {
                if (a[i] == ' ')
                {
                    sp = i;
                    break;
                }
            }
            for (int j1 = sp+1, j2=res.Length-1-s; j1 <= s; j1++, j2++)
            {
                res[j2] = a[j1];
            }
            if (!(sp == -1))
            {
                res[a.Length - 1 - sp] = ' ';
                ReverseWordInOrder(a, sp - 1, res);
            }
            return;
       }

Iteratively

public static string ReverseStringInOrder2(string x)
        {
            char[] res = new char[x.Length];
            int s = 0;
            int sp = -1;
            for (int i = 0; i <= x.Length - 1; i++)
            {
                if (x[i] == ' ')
                {
                    sp = i;
                    for (int j1 = s, j2 = res.Length - sp; j1 < sp; j1++, j2++)
                    {
                        res[j2] = x[j1];
                    }
                    res[res.Length - 1 - sp] = ' ';
                    s = sp+1;
                    sp = -1;
                }
            }
            if (sp == -1)
            {
                for (int m1 = s, m2 = 0; m1 < x.Length; m1++, m2++)
                {
                    res[m2] = x[m1];
                }
            }
            return new string(res);
        }

- TryToCode December 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java code using a Stack:

private static String stackToString(Stack<String> stack) throws IllegalArgumentException {
		if (stack==null){throw new IllegalArgumentException("null stack");}
		StringBuilder sb = new StringBuilder();
		
		while (!stack.isEmpty()){
			sb.append(stack.pop());
			if (!stack.isEmpty()){sb.append(' ');}
		}
		return sb.toString();
	}
	
	public static String reverseSentence(String s) throws IllegalArgumentException {
		if (s==null){throw new IllegalArgumentException("null string");}
		StringBuilder sb = new StringBuilder();
		Stack<String> stack = new Stack<>();
		
		for (int i=0;i<s.length();i++){
			if (s.charAt(i)==' '){
				stack.push(sb.toString());
				sb = new StringBuilder();
			}
			else {sb.append(s.charAt(i));}
		}
		stack.push(sb.toString());
		
		return stackToString(stack);
	}

- EulGam05 December 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursively (assuming I can use some helper methods like indexOf and that space is the only tokenizer and there aren't contiguous space)

String reverse(String s) {
		int pos = s.indexOf(" ");
		if(pos < 0)
			return s;
		else return reverse(s.substring(pos+1)) + " " + s.substring(0, pos);
	}

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

def func(string):
s=string.split(" ")
s=" ".join(s[::-1])
return s

- Rishav December 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why don't you just store the splitted words into a simple array and then iterate through this array until its middle part swapping 'i' element by 'i + length - i' ?

- Fernando December 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction:
"until its middle part swapping 'i' element by 'array.length - i' "

- Fernando December 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Another option is to traverse this same array backwards (from 'i = length' to 'i = 0').

- Fernando December 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here it is

public class ReverseSentence {

  public String get(String data) {
    if (data == null || data.length() <= 2)
      return data;

    char space = ' ';
    char[] src = data.toCharArray();
    char[] reversed = new char[data.length()];
    
    int pos = 0, end = data.length();
    
    for (int index = data.length() - 1; index >= 0; index--) {
      int start = -1;
      if (src[index] == space)
        // word starts next to space
        start = index + 1;
      else if (index == 0)
        start = 0;

      if (start != -1 && start <= end) {
        int len = end - start;
        // copy the word
        System.arraycopy(src, start, reversed, pos, len);
        end = index;
        pos += len;
        if (pos < data.length())
          // add space between words
          reversed[pos++] = space;
      }
    }
    return new String(reversed);
  }

  public static void main(String[] args) {
    ReverseSentence rs = new ReverseSentence();
    System.out.println(rs.get("repeat these words to reverse the sentence "));
  }
}

- geekyjaks December 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

A no additional buffer solution:

Use two parsers, p1, p2;

int p1 = 0, p2 = 0
	// this function parse through str from index p2 to find the index for next space
	p2 = searchNextSpaceIndex(str, p2) - 1
	while p2 != -1{
             while p1 < p2 {
	         swap(str, p1++, p2--)
	      }
		p1 = p2
		p2 = searchNextSpaceIndex(str, p2) - 1
	 }

Complexity :
Time : O(n)
Space: O(1)

- gameboy1024 December 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is just wrong. Check the task again.

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

Sorry, didn't pay attention.
Well, just do what the other said..
Do a reverse in O(n) time and O(1) space, then do what I posted.
Hopefully, still O(n) time and O(1) space

- gameboy1024 December 07, 2013 | Flag


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