Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Here is the solution in C++. Quite straightforward, and true the stack size would be O(n.size)

First call is PrintStrings("", n, 0, n.size)

const char phonePad[][5] =
{
	{'1', ' ', '\0', '\0', '\0'},
	{'3', 'a', 'b', 'c', '\0'},
	{'3', 'd', 'e', 'f', '\0'},
	{'3', 'g', 'h', 'i', '\0'},
	{'3', 'j', 'k', 'l', '\0'},
	{'3', 'm', 'n', 'o', '\0'},
	{'4', 'p', 'q', 'r', 's'},
	{'3', 't', 'u', 'v', '\0'},
	{'4', 'w', 'x', 'y', 'z'},
	{'1', '+', '\0', '\0', '\0'}
};

static void PrintStrings(std::string prefix, int n[], int startIndex, int size)
{
	const char* optionsStr = &phonePad[n[startIndex]][0];
	int options = atoi(optionsStr);
	for (int optionIndex = 1; optionIndex < 1 + options; ++optionIndex)
	{
		std::string newPrefix = prefix + phonePad[n[startIndex]][optionIndex];
		if (startIndex == size - 1)
		{
			printf("%s\n", newPrefix.c_str());
		}
		else
		{
			PrintStrings(newPrefix, n, startIndex + 1, size);
		}
	}
}

- gimli March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution. We just could use strlen(phonePad[i]) instead of (additional element phonePad[i][0] and atoi(phonePad[i][0])).

- Aleksey.M December 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How was it a trick question? If you are recursing, the space complexity is unlikely to be O(1). Is this where you messed up?

That said, I highly doubt you were rejected just because you got that one thing wrong. That's not how interview evaluating works.

- Gayle L McDowell March 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Gayle, Thank you for answering on my comment. My mind was really spinning about this question. I answered O(4^n) stack space - but I was sure I was wrong and he said don't you think its too high to claim. I struggled a bit - the correct answer the interviewer told me was O(n).
It really took some time for me to wrap my brain at the end of the interview.

I was looking for some clues in your book - "cracking the coding interview" to strengthen my ability to calculate the stack space for problems like these and other like
What will be the stack space requirement for a BFS on a balanced tree - I would recommend if possible have a small section to orient students like me on this topic a little)

(I consider your book to be my guide, mistakes if any are mine).

- whizz.comp March 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But yeah, my solution was correct, I checked it using a IDE if it would worked fine (after the interview) and the interviewer agreed it was correct. He then asked me the iterative solution (just concept - not to code) - I told we can use emulate recursion using a stack. He said OK and nothing else, and I was mistaken here too (I guess?) because this is really sub optimal. The really optimal solution is to iterate through and keep increasing the low value to high value and print number.
I did not mention the optimal solution as he seemed satisfied with my first thought. I could have come to the optimal strategy if he said - "try harder". Probably this was just a communication flow mismatch.

I think this was second mistake - probably enough to reject?

- whizz.comp March 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

clearly, if length of number is 10, than your recursive program will require a stack no greater than 10 elements. when stack becomes full it prints that combination and pops top chars to make space for next chars in the combination. remember same stack can be used multiple times. so space complexity will be O(n).

nyways, i m curious abt how you manage to print all combinations without using stack with recursive prog..

- napster March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe you were thinking of a backtracking solution by "keep increasing the low value to high value". If it was on your mind, you should have mentioned the idea. Seems a good optimization but could be annoying to program, especially in an interview. You would require a method which takes a string and gives the next string in the sequence. You would also require a smarter data structure to store the phone pad alphabets which would make it easier to query the next alphabet given the previous one.

- yossee March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Same idea as above but way simpler.


#define NUMWIDTH 7

char sary[][5] = {"abc\0","def\0","ghi\0","jkl\0","mno\0", "pqrs", "tuv\0", "wxyz"};
char getChar(int nNum, int nPos)
{
return sary[nNum-2][ nPos];
}
char getLen(int nNum)
{
return strlen(sary[nNum-2]);
}
void telephoneword( int nPos, string s)
{
if ( nPos ==NUMWIDTH)
{
printf("\t %s", s.c_str());
}
else
for ( int i = 2 ; i < 10 ; i++)
{
for ( int j=0; j < getLen(i) ; j++)
telephoneword( nPos + 1, s + getChar(i, j));
}
}

telephoneword( 0, "");

- moiskim March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems like you're building words for dial sequence '23456789', no? The original problem statement says "you're given a number" (dial sequence), which could be of various length.
I have the following code for this problem:

char pad[][5] = {" ", " ", "abc","def","ghi","jkl","mno", "pqrs", "tuv", "wxyz"};

size_t button_size(size_t button_num) {
    return strlen(pad[button_num]);
}

char button_char(size_t button_num, size_t char_num) {
    return pad[button_num][char_num];
}

void build_word(const size_t curr_pos, const string& dial_seq, string str) {
    if(curr_pos == dial_seq.length()) {
        cout << str << endl;
    } else {
        for(size_t i = 0; i < button_size(dial_seq[curr_pos] - '0'); ++i) {
            build_word(curr_pos + 1, dial_seq, str + button_char(dial_seq[curr_pos] - '0', i));
        }
    }
}

build_word(0, dial_seq, "");

- just_passing_through March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

cant this be solved using dp??
ie. if str[i]==str[i-1]&& str[i-2] && str[i]!='7'
ans[i]=ans[i-1]+ans[i-2]+ans[i-3];
else if str[i]==str[i-1]&& str[i-2] && str[i-3] && str[i]=='7'
ans[i]=ans[i-1]+ans[i-2]+ans[i-3]+ans[i-4];
else if str[i]==str[i-1]
ans[i]=ans[i-1]+ans[i-2];
else
ans[i]=ans[i-1];

ans=ans[n-1]

- :) April 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string.h>
char num[][5] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
void printchar(char * numstr, char * buf, int pos)
{

if(strlen(numstr))
{

int n = *numstr - 0x30;
char * chars = num[n];

if(strlen(chars))
{
while(*chars)
{
buf[pos] = *chars;
//printf("%c", buf[pos]);
printchar(numstr + 1, buf, pos + 1);
chars ++;
}
}
else
{
printchar(numstr + 1, buf, pos);
}
}
else
{
buf[pos] = '\0';
printf("%s\n",buf);
}
}

void main()
{
char *numstr = "91276";
char *buf = malloc(strlen(numstr));

printchar(numstr, buf, 0);
free(buf);
}

- akashj April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import sys, pdb

def telephone(n,pos,sz,ans):
    global trans, cnt

    if sz==0:
        cnt+=1
        print cnt,''.join(ans)
        return
        
    C=trans[n[pos]]
    for c in C:
        ans[pos]=c
        telephone(n,pos+1,sz-1,ans)

if __name__=='__main__':
    trans={'0':"",'1':"abc",'2':"def",'3':"ghi",'4':"jkl",'5':"mno",'6':"pqr",'7':"stu",'8':"vwx",'9':"yz"}
    
    num='12345600'
    cnt=0
    telephone(num,0,len(num),['']*len(num))

- light May 22, 2012 | 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