Microsoft Interview Question






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

Case 1:
Swap whole string in-place. Then swap substrings in-place.

Case 2:
Two unsigned integer iterators, left/right. left goes ->. right goes <-.

If A is a comma, increment left.
If B is a comma, increment right.
swap A,B in-place.
Do until right>left.

Case 3:
1. Put a comment about the bug in the CVS explaining in detail what the bug is and what I did.
2. Document the code.
3. Explain my algoritm during a code review.

- Jack March 24, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You could use one iterator instead of 2 but that wouldn't make a significant difference asymptotically.

- Jack March 24, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// first case
#include<stdio.h>
#include<string.h>
void rev(char * str, int i, int j)
{
char temp;

while(j != i + 1 && j != i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j--;
i++;
}
}

int main()
{
int strLen = 0;
char str[100];
int i = 0, j = 0 ;

strcpy(str , "a quick brown fox");
strLen = strlen(str);

for(i = 0; i < strLen; )
{

j = i;

while(str[j] != ' ' && str[j] != '\0')
{
j++;
}
// printf("%s, %d, %d\n", str, i, j);
rev(str, i, j - 1);
i = j + 1 ;
}

printf("\n%s\n", str);

return 1;
}



// second case ','
#include<stdio.h>
#include<string.h>
void rev(char * str, int i, int j)
{
char temp;

//printf("rev - %s, %d, %d\n", str, i, j);
while(j != i + 1 && j != i)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
j--;
i++;
}
}

int main()
{
int strLen = 0;
char str[100];
int i = 0, j = 0 ;

strcpy(str , "a quick, brown fox");
strLen = strlen(str);

for(i = 0; i < strLen; )
{

j = i;

while(str[j] != ' ' && str[j] != ',' && str[j] != '\0')
{
j++;
}
//printf("%s, %d, %d\n", str, i, j);
rev(str, i, j - 1);

while(str[j] == ' ' || str[j] == ',' || str[j] == '\0')
{

j++;
}
i = j;
}

printf("\n%s\n", str);

return 1;
}

- Rohit Ghatol April 15, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

looking for space and replacing the word without special letters like:
!,?"()[]{}

char newStr = new(strlen(sentence))
char *start = sentence
char *ptr = sentence

while (ptr!=NULL) && !(isspace(ptr)) ptr++;
if ptr == NULL
copyFromStartToPtr(start, ptr) //copy includes the '\0'
else
int strtIdx=0,endIdx=0;
while (!check(start+strtIdx) && ((start+strtIdx)!=ptr)) strtIdx++;
while (!check(ptr-endIdx)&&
((ptr-endIdx)!=ptr) &&
((start+strtIdx)!=ptr)) ) endIdx--;
if ((start+strtIdx)!=ptr))
switchTheWord(start+strtIdx, ptr-endIndx,newStr);
// copying the special characters
while (strtIdx)
{
// copy the special char
*(newStr+(start-sentence)+strtIdx)=*(ptr+strtIdx)
strtIdx--;
}
while (endIdx)
{
// copy the special char
*(newStr+ptr-start+endIdx)=*(sentence+ptr-start+endIdx)
endIdx--;
}

switch like in the previous code
check should check that the word start and ends with letters
hope its clear.
:)

- Eila June 11, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Test:
words:
" "
",,,,,,,,,,,,,,,,,,,, "
"............."
"!.,!.,"
"abhy,"
"asdf dfbsdfb ."
etc.

- Eila June 11, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Note the following code has various problems. Just to give you a general idea.
1. char* input = "This is "
"This is " is a constant so that the letters can not be changed. However,
char input[] = "This is ", the letters can be changed.
============================================

int main(){
..char* input = "This is a good day!";
..char* wordBegin;
..char* wordEnd;
..char tmp;
..char* output=input;
..while (input!='\0'){
....wordBegin=input;
....wordEnd=input;
....//step1: find begin of word
....while ((!(((*wordBegin)>='A') && ((*wordBegin)<='Z') || ((*wordBegin)>='a') && ((*wordBegin)<='z')))&& (wordBegin!='\0'))
......wordBegin++;

....//step2: find end of word
....wordEnd=wordBegin;
....while (((*(wordEnd+1)>='A') && ((*(wordEnd+1)<='Z')) || ((*(wordEnd+1)>='a') && ((*(wordEnd+1))<='z'))))
......wordEnd++;
....input=wordEnd;
....//step3:swap letter by letter
....while (wordBegin<wordEnd){
......tmp=*wordEnd;
......//*wordEnd=*wordBegin;
......*wordBegin=tmp;
......wordBegin++;
......wordEnd--;
....}
......if (input!=0)
........input++;
..}
}

- XYZ September 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is the working code.
-------------------------------------
int main()
{
..string input = "This is a good day !!";
..int wordBegin=0;
..int wordEnd=0;
..int iterator=0;
..char tmp;

..while (iterator<input.size()){
....wordBegin=iterator;
....wordEnd=iterator;

....//step1: find begin of word
....while ((!((input[wordBegin]>='A') && (input[wordBegin]<='Z') || ((input[wordBegin]>='a') && (input[wordBegin]<='z')))&& wordBegin<input.size()))
......wordBegin++;

//step2: find end of word
....wordEnd=wordBegin;
....if (wordEnd<input.size()){
......while ((((input[wordEnd+1]>='A') && ((input[wordEnd+1]<='Z')) || ((input[wordEnd+1]>='a') && ((input[wordEnd+1])<='z')))))
........wordEnd++;
......iterator=wordEnd;
....}

....//step3:swap letter by letter
....while (wordBegin<wordEnd){
......tmp=input[wordEnd];
......input[wordEnd]=input[wordBegin];
......input[wordBegin]=tmp;
......wordBegin++;
......wordEnd--;
....}
....iterator++;
..}
..cout<<input;
}

- XYZ October 02, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here is the better code, copied from
http://www.careercup.com/question?id=62591
=================================================
void rev(char *r,char *l)
{
..char temp;
..while(r<l)
..{
....temp=*r;
....*r=*l;
....*l=temp;
....r++;
....l--;
..}
}

void revword(char *str)
{
..char *end,*x,*y;
..for(end=str;*end;end++);
..//reverse complete sentence
..rev(str,end-1);
..//reverse word in the sentence
..x=y=str;
..while(x++<end)
..{
....if(*x==' '||*x=='\0')
....{
......rev(y,x-1);
......y=x+1;
....}
..}
}
int main()
{
//Note here, char*s does not work, because *a is in read only const area, while s[]is in stack,changeable by rev().
//char *s="my name is anthony";
..char s[]="my name is anthony";
..revword(s);
..printf("\nafter rev : %s",s);
..return 0;
}

- XYZ November 21, 2008 | Flag


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