Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

To compress string you can use something called run-length coding. This makes use of the redunduncies (like repetitions in string) and compresses it.
for example:
if you have aaaabbbcddeeeeffff
it can compressed as \4a\3bc\2d\4e\4f.
the backslash is used as an escape sequence to denote that its a compressing element. you escape backslash in the original text with another backslash just like how you would do in c/c++.
you can do this in n steps. its a straightforward algorithm

- bleep February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would like to discuss why and if Huffman encoding will be a right answer? Run-length encoding is good if all the characters have equal probability of occuring. So for example - if a occurs 5 times and all other characters occur within the range of 3-6, then run length encoding is surely the right answer.

- Ketan February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Huffman coding is usually used when you need to transmit an alphabet (set of symbols, in this case the english alphabet) over a communication medium using binary numbers (usually) or a much smaller alphabet. Which is why i see doing huffman code as unnecessary, although its not wrong. And i was asked this question in a microsoft interview for the same position and they were looking for the same answer. :)

- bleep February 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Bleep you are right - huffman is overkill. bleep can you share more about your MS interview. You can mail me - whizz.comp at gmail

- ketan March 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Bleep, just out of curiosity, how is the original string reconstructed at the recieving end. can you illustrate the logic with a sentence..? Thanks in advance

- Srivathsan April 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

even equal probability may not result into actual compression of a string.
Consider any string which contains characters either only once or twice.
For e.g. "abcdefghijk". Run-length will in actual expand the string :)

- Sunil Singhal April 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <cstdlib>
#include <sstream>
#include <iostream>

int
main(int argc, char **argv)
{
    if(argc != 2) {
        std::cerr << "the string is required" << std::endl;
        return (EXIT_FAILURE);
    }   
    std::stringstream buf;
    char *pcur = argv[1];
    while(*pcur != '\0') {
        char cur_char = *pcur;
        int count = 1;
        pcur += 1;
        while(*pcur != '\0' && *pcur == cur_char) {
            count += 1;
            pcur += 1;
        }   
        buf << cur_char << count;
    }   
    std::cout << buf.str() << std::endl;
    return (EXIT_SUCCESS);
}

- smffap February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void compress(char*a)
{
int count=0;
char c=a[count];
int symcount=0;
while(a[count]!='\0')
{
while(a[count]==c)
{
symcount++;
count++;
}
if(symcount==1)
{
cout<<c;
}
else{
cout<<'/'<<symcount<<c;
}
symcount=0;
c=a[count];
}
}

- luckycat February 29, 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