Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 4 vote

String reverse(String s){
String[] terms = s.split("[ ]+");
String result="";
for(String term:terms){
result = term+" "+result;
}
return result;
}

- jose September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>
#include<string.h>
//Reversing the string in a special way 

void reverseString( char string[] ){
  int len,i; char temp;
  len = strlen(string);
  for(i=0;i<len/2;i++){
     temp = string[i];
     string[i] = string[len-i-1];
     string[len-i-1]= temp;
  }
}

void reverseWord(char *ptr, int wordLen){
  ptr =ptr-1;
  while(wordLen>0){
    printf("%c",*ptr);
    ptr=ptr-1;
    wordLen--;
  } 
 printf(" ");
}

void specialReverse( char string[]){
  reverseString(string);
  //Now that we have reverse the whole string
  char *ptr; int numChar=0;
  ptr=string;
  printf("");
  while( *ptr !='\0'){  
        while(( *ptr !='\0' ) &&(*ptr !=' ')){
             ptr = ptr+1;
             numChar++;
        }
        reverseWord(ptr,numChar);
        numChar=0;
        ptr=ptr+1;
  }
}



main(){
  char string[150];
  printf("Please enter the sentence for special reversal: ");
  scanf("%[^\n]", string);
  printf("\nYou have entered \" %s \" \n",string);
  specialReverse(string);
  return 0;
}

- Anonymous September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

- Reverse the entire String
- Reverse individual words of the string

Example:
"Welcome to hell"

After step 1:
"lleh to emocleW"

After step 2:
"hell to Welcome"

- chandershivdasani September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

#include<stdio.h>
#include<string.h>
//Reversing the string in a special way 

void reverseString( char string[] ){
  int len,i; char temp;
  len = strlen(string);
  for(i=0;i<len/2;i++){
     temp = string[i];
     string[i] = string[len-i-1];
     string[len-i-1]= temp;
  }
}

void reverseWord(char *ptr, int wordLen){
  ptr =ptr-1;
  while(wordLen>0){
    printf("%c",*ptr);
    ptr=ptr-1;
    wordLen--;
  } 
 printf(" ");
}

void specialReverse( char string[]){
  reverseString(string);
  //Now that we have reverse the whole string
  char *ptr; int numChar=0;
  ptr=string;
  printf("");
  while( *ptr !='\0'){  
        while(( *ptr !='\0' ) &&(*ptr !=' ')){
             ptr = ptr+1;
             numChar++;
        }
        reverseWord(ptr,numChar);
        numChar=0;
        ptr=ptr+1;
  }
}



main(){
  char string[150];
  printf("Please enter the sentence for special reversal: ");
  scanf("%[^\n]", string);
  printf("\nYou have entered \" %s \" \n",string);
  specialReverse(string);
  return 0;
}

- happy_feet September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string ReverseString(string OriginalString)
        {   
            Stack<string> myStack = new Stack<string>();
            string strBuff = "";
            for (int i = 0; i < OriginalString.Length; i++)
            {
                if (!OriginalString[i].Equals(' '))
                {
                    strBuff += OriginalString[i];
                }
                else
                {
                    myStack.Push(strBuff);
                    strBuff = "";
                }
            }
            myStack.Push(strBuff);
            strBuff = "";
            do
            {
                strBuff += myStack.Pop();
                strBuff += " ";
            } while (myStack.Count > 0);
            return strBuff;
        }

- I Rock September 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

tag

- jiangok2006 September 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi, i was also asked the same question in my interview. i gave the same approach(reversing string and then reversing each word.). but interviewer was keen on getting a better optimized solution. can someone help me on this??

- sathish July 24, 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