Microsoft Interview Question for Software Engineer in Tests






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

String removeDuplicate(string str)
{
int len=str.Length;
if(len<2) return str;
int tail=1;
for(int i=1;i<len,i++)
{
for(int j=0;j<tail;j++)
{
if(str[i]==str[j])
break;
if(j==tail)
{
str[tail]=str[j];
++tail;
}
}
}
str[tail]='/0';
}

- ohioboy January 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am wondering if this algorithm is O(n^2) or O(n^3)??

- Anonymous January 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it is O(n^2). If you want O(n), you can use hash table.

- ohioboy January 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@kumar palike...
were u interviewed in India?which collg are u from?is microsoft hiring in India?

- dream January 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes Microsoft is hiring in India. It came to all the IITs and IT BHU and IIIT Hyderabad, IIIT Allahabad.

- spsneo January 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Abe ma k lode.... NITs ko bhul gaya??

- Vikas Ki ma ki chuut August 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string removeDuplicate(char s[])
{ for (int i=0; i< strlen(s) ; i++)
{
for (int j=i+1; j< strlen(s) ; j++)
{
if(s[i] == s[j]){
int count=j;
while(s[count]!=0)
{
s[count] = s[count+1];
count++;
}
}
}
}

std::cout<<s;
return s;

}

- Vipul K. January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code works, but can someone tell me whats the complexity of this function now??

- Anonymous January 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Haha!

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

Complexity is n^2 for the one that Vipul did , but this can be done using hashtable in O(n)

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

use 256 bit map to track which character has been occurred. so your hash table wont take that much space, you can do this with 16 byte extra space. and O(n)

- alok January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void print(string s)
{
    int i; 
    bool V[256];
     
     memset(V,false,sizeof(V));
     
    for(i=0;i<s.size();i++)
      V[(int)s[i]]=true;
      
    for(i=0;i<s.size();i++)
     {
        if( V[(int)s[i]] )
         {
          cout<<s[i];
          V[(int)s[i]]=false;
          
          }
          
          }
          
    return;
    
}
T.C=O(n) & S.C=O(1) as we need 256 booleans for ASCII strings independent of the size of string .

- JD January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

generally they extend this question to the case of unicode characters after u use an "array method"

- dream January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How to approach it then ?

- @ dream January 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In case of unicode chars, we can convert them to int using library functions. Can somebody confirm if this is the best approach?

- abhispra January 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use HashSet of <char>,get the result in 0(n)

- sanj January 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i guess Hashset is one of the methods...guys any other method?

- dream January 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'll just use std::map.

string s = "This is a test, let's see.";
map<char, int> m;
int index = 0;
for(int i=0;i<s.length();++i)
{
char c = s[i];
if(m[c] == 0)
{
m[c] = 1;
s[index] = c;
++index;
}
}

for(int i=index;i<s.length();++i)
s[i] = ' ';

- Bingsuns January 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use of map is the ultimate chutiyapa here...u slopy...

- maibhihun January 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use hashing key as char , and value as count, if count more than 1 discard the char

- Anonymous February 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1 #include <stdio.h>
2 #include <string.h>
3
4 void delete_repeated_chars(char * s)
5 {
6 int n = strlen(s);
7 int repeated[128];
8 int i;
9 for(i=0; i<128; i++)
10 repeated[i] = 0;
11 int readpos = 0;
12 int writepos = 0;
13 for(; readpos<=n; readpos++)
14 {
15 if(!repeated[s[readpos]]&&writepos!=readpos)
16 {
17 repeated[s[readpos]] = 1;
18 s[writepos++] = s[readpos];
19 }
20 }
21 s[writepos] = '\0';
22
23 }
24

- Kyra February 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
void rev(char *);
int main()
{
char str[100];
printf("Enter a string\n");
gets(str);
printf("Original string\n");
printf("%s\n",str);
rev(str);
return 0;
}

void rev(char * str)
{
int len;
len=strlen(str);
char hash[256];
int j;
for(j=0;j<256;++j)
hash[j]=0;
char *nstr=(char *)malloc(sizeof(char)*len);
int i;
j=0;
for(i=0;i<len;++i)
{
if(hash[((int)str[i])]==0)
{
hash[((int)str[i])]=1;
nstr[j++]=str[i];
}
}
printf("\n%s\n",nstr);
}

- gautam kumar February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
void rev(char *);
int main()
{
char str[100];
printf("Enter a string\n");
gets(str);
printf("Original string\n");
printf("%s\n",str);
rev(str);
return 0;
}

void rev(char * str)
{
int len;
len=strlen(str);
char hash[256];
int j;
for(j=0;j<256;++j)
hash[j]=0;
char *nstr=(char *)malloc(sizeof(char)*len);
int i;
j=0;
for(i=0;i<len;++i)
{
if(hash[((int)str[i])]==0)
{
hash[((int)str[i])]=1;
nstr[j++]=str[i];
}
}
printf("\n%s\n",nstr);
}

- gautam kumar February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char a[20], ch;
int i, j, len = 0;

puts("Enter a string");
while(1)
{
scanf("%c", &ch);
if(ch == 10)
break;
else
{
a[len] = ch;
len++;
}
}
a[len] = '\0';

for(i = 0;i < len; i++)
{
for(j = i - 1; j>= 0; j--)
{
if(a[i] == a[j])
break;
}
if(j < 0)
printf("%c", a[i]);
}

- Ranganath March 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

:) it's established that coding the favorite activity amongst most visitors.
A very vital part of the excercise though is test cases for the method.

here are some :

aaaaa
aaaabbbbb
abcd - all unique
abababab - alternating to check if sequence is maintained along with uniqueness
aaaaab aaaa
null

any other suggestions ??

- Rohit June 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Public Class Helper{
Public Static string removedup(string sentence)
{
return new string(sentence.ToCharArray().Distinct().ToString())
}
}

Main routine to call the function, write in the main :->
{
Console.WriteLine(Helper.removedup("This is a String"));
}
O/p= This a trng

This is using Linq Approach in C#

- Water October 21, 2010 | 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