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

Comment hidden because of low score. Click to expand.
2
of 2 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

XOR does work, no need to check duplicated characters:

``````public class ID62240
{
public void reverse(char[] string)
{// swap two numbers without temp variables
int n=string.length;
int left=0;
int right=n-1;
while (left<right)
{
// swap
string[left]=(char)(string[left]^string[right]);
string[right]=(char)(string[left]^string[right]);
string[left]=(char)(string[left]^string[right]);
// next
left++;
right--;
}
}

public static void main(String[] args)
{
ID62240 wpd=new ID62240();
String input="abcdefg12a";
char[] string=input.toCharArray();
wpd.reverse(string);
for (char c: string)
System.out.print(c);
System.out.println();
}

}``````

Comment hidden because of low score. Click to expand.
2
of 2 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.
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.
0
of 2 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.
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.
0
of 0 vote

``````class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to reverse");
original = in.nextLine();

int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);

System.out.println("Reverse of entered string is: "+reverse);
}
}``````

}

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

``````public class ReverseRec {

public static void main(String[] args){

String str=new String();

str="greetings";

ReverseRec rev=new ReverseRec();

String newstr=new String();

System.out.print(rev.reverseRec(str));

}

String reverseRec(String str){

if(str.length()==1){

return str;

}

else {

str= reverseRec(str.substring(1)) + str.charAt(0);

}

return str;
}

}``````

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

Solution in C#

``````public static string Reversed(string str)
{
int length = str.Length;
for (int i = 0; i < length; i++)
{
str += str[length - (i + 1)];
}
str = str.Substring(length);
return str;
}``````

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

Java solution:

``````String reverseString(String s)
{
int size=s.length();
boolean isEven;

char[] myCharAry=s.toCharArray();

if(size%2==0)
{
isEven=true;
}
else
{
isEven=false;
}

if(isEven)
{

for(int i=0; i<(size/2); i++)
{
char temp;
//right side
temp=myCharAry[(size/2)+i];
//swap
myCharAry[(size/2)+i]=myCharAry[(size/2)-1-i];
myCharAry[(size/2)-1-i]=temp;
}
}
else
{
int indexOfMid=size/2;
for(int i=0; i<(size/2); i++)
{
char temp;
//right side
temp=myCharAry[size/2+1+i];
myCharAry[(size/2)+1+i]=myCharAry[(size/2)-1-i];
myCharAry[(size/2)-1-i]=temp;
}

}
return new String(myCharAry);

}``````

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

/* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static StringBuffer sbf = new StringBuffer("Hello");
public static void main (String[] args) throws java.lang.Exception
{

int high=sbf.length()-1;
int low =0;
while(low<high)
reverse((int)sbf.charAt(low),low++,(int)sbf.charAt(high),high--);

System.out.println(sbf.toString());
}
static void reverse(int a,int i,int b,int j)
{
a = a+b;
b = a-b;
a = a-b;
sbf.setCharAt(i,(char)a);
sbf.setCharAt(j,(char)b);
}
}

}

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

public static void main(String[] args) {

String s = "workshop";
int length = s.length();
for(int i=s.length()-1;i>=0;i--) {
s+=s.charAt(i);
}

s = s.substring(length);
System.out.println(s);
}

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

``````public static void main(String[] args) {

String s = "workshop";
int length = s.length();
for(int i=s.length()-1;i>=0;i--) {
s+=s.charAt(i);
}

s = s.substring(length);
System.out.println(s);``````

}

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

``````public static void main(String[] args) {

String s = "workshop";
int length = s.length();
for(int i=s.length()-1;i>=0;i--) {
s+=s.charAt(i);
}

s = s.substring(length);
System.out.println(s);
}``````

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

``````public static void main(String[] args) {

String s = "workshop";
int length = s.length();
for(int i=s.length()-1;i>=0;i--) {
s+=s.charAt(i);
}

s = s.substring(length);
System.out.println(s);
}``````

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

``````for (int i = len/2; i < len; i++) {
s[i] = (char) (s[i] + s[len - i - 1]);
}``````

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

``````public static void main(String args[]) {
char[] s = {'s','t','r','n','g'};
int len = s.length;
int j = len;
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[i]- s[len - i - 1]);
}
for (int i = (len%2==0)?(len/2):(len/2+1); i < len; i++) {
s[i] = (char) (s[i] + s[len - i - 1]);
}
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[len - i - 1] - s[i]);
}

for (int i = 0 ; i < len ; i++) {
System.out.println (i+":"+s[i]);
}

}``````

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

public static void main(String args[]) {
char[] s = {'s','t','r','n','g'};
int len = s.length;
int j = len;
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[i]- s[len - i - 1]);
}
for (int i = (len%2==0)?(len/2):(len/2+1); i < len; i++) {
s[i] = (char) (s[i] + s[len - i - 1]);
}
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[len - i - 1] - s[i]);
}

for (int i = 0 ; i < len ; i++) {
System.out.println (i+":"+s[i]);
}

}

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

``code``

public static void main(String args[]) {
char[] s = {'s','t','r','n','g'};
int len = s.length;
int j = len;
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[i]- s[len - i - 1]);
}
for (int i = (len%2==0)?(len/2):(len/2+1); i < len; i++) {
s[i] = (char) (s[i] + s[len - i - 1]);
}
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[len - i - 1] - s[i]);
}

for (int i = 0 ; i < len ; i++) {
System.out.println (i+":"+s[i]);
}

}

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

``````public static void main(String args[]) {
char[] s = {'s','t','r','n','g'};
int len = s.length;
int j = len;
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[i]- s[len - i - 1]);
}
for (int i = (len%2==0)?(len/2):(len/2+1); i < len; i++) {
s[i] = (char) (s[i] + s[len - i - 1]);
}
for (int i = 0; i < len/2; i++) {
s[i] = (char) (s[len - i - 1] - s[i]);
}

for (int i = 0 ; i < len ; i++) {
System.out.println (i+":"+s[i]);
}``````

}

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

int j = 1

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

``int j = 1;``

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

e

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

Use the ASCII value to swap the chars ..

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

\$input = \$_POST['name'];
\$words = strrev(\$input);
print \$words;

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

Should not use inbuilt functions.

Comment hidden because of low score. Click to expand.
-2
of 2 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;
}

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

If you add 'A' - 65 + ' ' - 32. you will get 97 i.e. 'a'

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.

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