## Apple Microsoft Interview Question Software Engineer / Developers Software Engineer in Tests

• 1

Reverse a string , . don't use any temp variable to store the string .

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

Anyone has a solution to this one? Does it mean we can't use any external memory? Then we have to use XOR to exchange two characters, right?

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

use the XOR
try this
int a = 'a', b = 'b';
System.out.println(Integer.toBinaryString(a));
System.out.println(Integer.toBinaryString(b));
a = a^b;
System.out.println(Integer.toBinaryString(a));
b = a^b;
System.out.println(Integer.toBinaryString(b));
a = a^b;
System.out.println(Integer.toBinaryString(a));

Comment hidden because of low score. Click to expand.
0

Nope, that won't work. Consider having the same character. Then, if you xor any two characters, you would get a 0 since xor only returns 1 if one of the other is different. So, 00 returns 0, 11 returns 0. 01 returns 1 and 10 returns 1

Comment hidden because of low score. Click to expand.
0

Simple, just add a special case for characters that are equal. If so, do nothing. Otherwise, flip em with xor.

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

void swap (char &a, char &b){a = a ^ b;b = a ^ b;a = a ^ b;}
void reverse(string &s){for(int i=0;i<s.length()/2;i++)swap(s[i],s[s.length()-i-1]);}
int main()
{
string s;cin>>s;
reverse(s);
cout<<s<<endl;
system("pause");
}

Comment hidden because of low score. Click to expand.
0

Good solution, but it's really ugly.

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

#include <stdlib.h>
#include <stdio.h>

void reverse(char* str)
{
int i, j, len;
char temp;
i=j=len=temp=0;

j=strlen(str) - 1;
while(i < j)
{
str[i] = str[i] ^ str[j];
str[j] = str[i] ^ str[j];
str[i] = str[i] ^ str[j];
i++;j--;
}
}

int main()
{
char str[]="hello world";
reverse(str);
printf("%s",str);
getch();
}

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

Use the ASCII value to swap the chars ..

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

//inplace reverse
#include<iostream>
using namespace std;

void main()
{
int length=0;;
char str[7]= "abcdef";
while(str[length] != '\0')
length++;
int i=0;
while(i<length/2)
{
str[i] = str[i] + str[length - i-1];
str[length -i-1] = str[i]- str[length - i-1];;
str[i] = str[i] - str[length -i-1];
i++;
}
cout<<str;
}

Comment hidden because of low score. Click to expand.
0

This might result in overflow

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

void reverse( char *s) {
int i,n=strlen(n);
char *p=s+n-1;
for (i=0;i<n/2; i++) {
*s = *s ^ *p;
*s = *s ^ *p;
*s = *s ^ *p;
s++; p--;
}
}

Comment hidden because of low score. Click to expand.
0

this approach is best but suffer when 2 char's are same because a^a=0. Adding a if statement inside the loop will make solution complete.

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

// skipping same chars
while(i<length/2)
{
if (str[i] != str[j])
{
str[i] = str[i] + str[length - i-1];
str[length -i-1] = str[i]- str[length - i-1];;
str[i] = str[i] - str[length -i-1];
}
i++;
}

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

use int j=length - i-1

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

Generally, with this question the interviewer wants to see how you deal with recursion. For extra marks, you should modify it to the tail-recursive version (Google "tail recursion").

For Java engineers, here's mine - I convert to char array to avoid multiple substring calls ... and to make the tail recursion easier:

``````public String reverse(String text) {
return reverseHelper(text.length() - 1, text.toCharArray());
}

private String reverseHelper(int index, char[] text) {
return index < 0? "" : text[index] + reverseHelper(index - 1, text);
}``````

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

Oh, I forgot to add ... now can you write a function to reverse the words in a string. In other words "This version of Anonymous is the greatest coder on the planet" should be "planet the on coder greatest the is Anonymous of version This"

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

``````#include <stdio.h>
#include <string.h>

void reverse(char *stringToReverse);

int main (int argc, const char * argv[]) {
char str[] = "fours";

reverse(str);
printf("%s", str);
return 0;
}

void reverse(char *stringToReverse) {
int start = 0;
int temp = strlen(stringToReverse);
int end = temp - 1;

while (end >= start) {
stringToReverse[temp] = stringToReverse[start];
stringToReverse[start] = stringToReverse[end];
stringToReverse[end] = stringToReverse[temp];
start++;
end--;
}

stringToReverse[temp] = '\0';
}``````

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

<pre lang="" line="1" title="CodeMonkey52687" class="run-this">public class ReverseString {

public static void main(String args[])
{
String str = "";
String reversedStr = "";
Scanner input = new Scanner(System.in);
input.useDelimiter(System.getProperty("line.separator"));
System.out.println("Enter a string to reverse: ");
str = input.next();
if(str != null && str.trim().length()!=0)
{
reversedStr = reversedStr.concat(str.substring(str.length()-1));
for(int i=str.length()-2; i>=0; i--)
{
System.out.println(reversedStr);
reversedStr = reversedStr.concat(str.substring(i, i+1));

}
}
System.out.println(reversedStr);
}
}</pre>

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

Java code Eg: revString("Submit".toCharArray(), 0, 5)

``````public static void revString(char[] charArr, int start , int end){
while(start < end){
if(charArr[start] != charArr[end]){
charArr[start] = (char) (charArr[start] + charArr[end]);
charArr[end] = (char) (charArr[start] - charArr[end]);
charArr[start] = (char) (charArr[start] - charArr[end]);
}
start++;
end--;
}``````

}

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

Is the question misunderstood? The question asks not to use extra storage for the string and not for the individual character.

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

Use recursive function
*sPtr is the pointer to the input string

void reverse(char *sPtr)
{
if (*sPtr[0]!='\0')
{
reverse(&sPtr[1]);
printf("%c",*sPtr[0]);
}
else
{
return;
}
}

Comment hidden because of low score. Click to expand.
0

good idea, however, some mistakes,

void reverse(char *sPtr)
{
if (sPtr[0]!='\0')
{
reverse(&sPtr[1]);
printf("%c",sPtr[0]);
}
else
{
return;
}
}

int main()
{
char *a = "abcdef";
reverse(a);
}

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

Just use the swap utility function without using a temp variable, for swapping two characters on wither end of the string ..

void swap (char a, char b)
{
a = a + b;
b = a - b;
a = a - b;
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### 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.