Amazon Interview Question for Software Engineer / Developers






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

Iterative is better by swapping. No extra space. Recursive, stack is used, but simple.

- Messi April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseStringTest {
  public static void main(String[] args) {
    String str = "What's going on?";
    System.out.println(ReverseString.reverseIt(str));
  }
}

class ReverseString {
  public static String reverseIt(String source) {
    int i, len = source.length();
    StringBuffer dest = new StringBuffer(len);

    for (i = (len - 1); i >= 0; i--)
      dest.append(source.charAt(i));
    return dest.toString();
  }
}

- Anonymous April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude! I am confused. Why don't you simply use StringBuffer's reverse() method? I think when somebody asks for reversing a string they meant without library functions. They want us to work a bit. Here is the simple one with temporary variable.
{public static String getReverse(String input) {
char c[] = input.toCharArray();
int len = c.length - 1;
if (len <= 0) {
return input;
}
for (int i = 0; i < len / 2; i++) {
char temp = c[len - i];
c[len - i] = c[i];
c[i] = temp;
}
return new String(c);
}}

- Gk July 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String strRev(String str){

StringBuffer strBuf = new StringBuffer();

for(int i = str.length() - 1 ; i >= 0 ; i--){
strBuf.append(str.charAt(i));
}

return strBuf.toString();

}

public static String strRevRecursive(char[] str, int begin, int end){

if(begin >= end){
return String.valueOf(str);
}else{
char temp = str[begin];
str[begin] = str[end];
str[end] = temp;
return strRevRecursive(str, begin+1, end-1);
}

}

- A January 20, 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