Amazon Interview Question: Reverse a string... | Glassdoor

Interview Question

Quality Assurance Engineer Interview Seattle, WA

Reverse a string

Answer

Interview Answer

5 Answers

0

Using Python :

def reverse(string):
    L = []
    count = 1
    for i in range(0,len(string)):
        L.append(string[len(string)-count])
        count += 1
    L_reverse = ''.join(L)
    return L_reverse

Anonymous on Apr 28, 2014
0

public String reverseMe(String string1) {
      int length = string1.length();
      char[] charArray = new char[length];
      int j = 0;
      for(int i = length -1 ; i >= 0 ; i--) {
        charArray[j++] = string1.charAt(i);

      }

      String string2 = new String(charArray);
      return string2;
    }

Vicky D on May 4, 2014
0

String n='he'
for (int i=n.length-1; i--;i>=0){
  String n_rev = n.CharAt(i)+n;
}
System.out.println(n_rev);

FNU Madhulika on May 13, 2016
0

// Converted the String into Char array
        char[] a = str.toCharArray();
        // create a new String
        StringBuilder sb = new StringBuilder();
        // Started a loop
        for (int i = a.length - 1; i >= 0; i--) {
            // Add character
            sb.append(a[i]);
        }
        // Print out the string
        System.out.println(sb);

Nitesh on Apr 16, 2017
0

Ruby :

a = "This test is simple".downcase
b = a.chars.to_a
temp = []
len = b.length
for i in 1...len+1
  temp.push(b[-i])
end

f = temp.join
print f

Output: elpmis si tset siht

Using Ruby : on May 15, 2017

Add Answers or Comments

To comment on this, Sign In or Sign Up.