Apple Microsoft Interview Question 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));

- Colin on February 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous on September 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous on January 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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();
	}

}

- wangpidong on October 15, 2013 | Flag
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");
}

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

Good solution, but it's really ugly.

- KachikosBodkin on February 20, 2013 | Flag
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();
}

- Anonymous on February 25, 2011 | Flag Reply
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?

- Anonymous on February 13, 2009 | Flag Reply
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;
}

- Anonymous on February 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This might result in overflow

- HiThere on March 18, 2009 | Flag
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--;
}
}

- sirish on April 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous on December 11, 2010 | Flag
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++;
}

- kulang on June 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use int j=length - i-1

- kulang on June 11, 2009 | Flag Reply
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);
    }

- Anonymous on January 15, 2010 | Flag Reply
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"

- Anonymous on January 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about:

#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';
}

- Chris on March 30, 2011 | Flag Reply
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>

- bee on May 14, 2011 | Flag Reply
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--;
		}

}

- Say on March 13, 2012 | Flag Reply
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.

- VR on June 04, 2012 | Flag Reply
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);
   }
}

}

- Saran on November 26, 2013 | Flag Reply
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;
    }

}

- megha on December 23, 2013 | Flag Reply
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;
        }

- k on August 20, 2014 | Flag Reply
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);
			
	}

- ATuring on August 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use the ASCII value to swap the chars ..

- Ganesh on October 08, 2008 | Flag Reply
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;
}
}

- Waseem K. on October 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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);
}

- isisme on December 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- Justin on March 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Should not use inbuilt functions.

- Srigopal Chitrapu on May 01, 2014 | Flag
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;
}

- A^2 - Apple Aspirant on June 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Srigopal Chitrapu on May 01, 2014 | Flag


Add a Comment
Name:

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

Books

is a comprehensive book walking you through 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