Microsoft Interview Question for Software Engineer in Tests






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

The following codes does run length encoding and decodes its too...

#include<stdio.h>
void reverseRLE(char *);
void reverse(char *, int , int);
void main()
{
	char ch, s[10000],str[100];
	int i, count = 0, start = 0, end = 0;
	printf("Enter the string \n");
	gets(s);
	ch = s[0];
	i = 1;
	count = 1;
	int j = 0;
	while( s[i] != '\0')
	{
		if(s[i] == ch)
		{   
			//counting no. of occurances
			i++;
			count++;
		}
		else
		{
			//storing the count and the charcter in an array
			printf("%d%c ", count, ch);
			start = j;
			while(count/10 != 0)
			{
				str[j++] = count % 10 + '0';
				count = count/10;
			}
			str[j++] = count + '0';
			end = j-1;
			reverse(str, start, end);
			str[j++] = ch; 
			ch = s[i];
			count = 1;
			i++;
		}
	}
	printf("%d%c ", count, ch);
	if(str[0] != '\0')
	{
		start = j;
		while(count/10 != 0)
		{
			str[j++] = count % 10 + '0';
			count = count/10;
		}
		str[j++] = count + '0';
		end = j-1;
		str[j++] = ch; 		
		str[j] = '\0';	
		reverse(str, start, end);
	}	
	reverseRLE(str);	
}

void reverse(char *s, int start , int end)
{
	int temp = 0;
	while(start < end)
	{
		temp = s[end] - '0';
		s[end] = s[start];
		s[start] = temp + '0';
		start++;
		end--;

	}
}
void reverseRLE(char * s)
{
	int count=0, j = 0, i = 0;
	while(s[i] != '\0')
	{
		if(s[i] > '0' && s[i] < '9')
		{
			count = count*10 + (s[i]-'0');
			j++;
		}
		else
		{
			for(int k =0;k<count;k++)
				printf("%c",s[i]);
			count = 0;
		}
		i++;
	}
	for(int k =0;k<count;k++)
		printf("%c",s[i]);
}

- Gaurav April 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Another approach.

// run_len_encoding.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>

char temp = '\0';
int count = 0; 


void RLE(char ch)
{
if (temp == '\0')
{
count = 1;
temp=ch;
}

else
if( ch==temp)
{
count++;

} // end if

else
if(ch!=temp)
{
if(count == 1)
printf("%c",temp);//output previous character thru temp.
else if (count == 2)
printf("%c%c", temp,temp); 
else if(/*count > 2 && */count <= 255)
printf("$%d%c", count,temp);
else
printf("$255%c$%d%c",temp,count-256,temp);

temp=ch;
count=1;
}//if ch!=temp
	
}//RLE
int _tmain(int argc, _TCHAR* argv[])
{
	char character = 'a';
	printf("Enter characters one after the other:\n");

	while(character!= '.')
	{	
	character = getchar();
	RLE(character);
	}

	return 0;
}

- Game April 05, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void runLengthEncoding(char s[]){
int count=1, i =0, index = 0,j=0;
int len = strlen(s);
while(i < len){
if(s[i] == s[i+1]){
s[index++] = s[i];
count++;
// index = i + 1;
i++;

while(s[i] == s[++i]){
count++;
}
char p = count + '0';//count is an integer, changing it to character(by adding ASCII value of 0
s[index++] = p;
count =1;
}
else{
s[index++] = s[i++];
}

}
s[index] = '\0';
}

- Nikunj October 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The line (index = i+ 1) and
integer j is not required

void runLengthEncoding(char s[]){
int count=1, i =0, index = 0;
int len = strlen(s);
while(i < len){
if(s[i] == s[i+1]){
s[index++] = s[i];
count++;
i++;

while(s[i] == s[++i]){
count++;
}
char p = count + '0';//count is an integer, changing it to character(by adding ASCII value of 0
s[index++] = p;
count =1;
}
else{
s[index++] = s[i++];
}

}
s[index] = '\0';
}

- Nikunj October 15, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The simplest approach i could think of !!

#include<stdio.h>

int main()
{

float hr, min, angle;

char line[] = "aaabbbbcc";
int i,cnt=1;

for (i=1;i<=strlen(line);i++)
{
if(line[i] == line [i-1])
cnt++;
else
{
printf(" %c %d \n",line[i-1],cnt);
cnt=1;
}
}


return 0;
}

- Sarz November 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string s;cin>>s;int i=0,l=s.length();
while(i<l)
{
int count=1;
while(i<l && s[i]==s[i+1]){count++;i++;}
cout<<count<<s[i];i++;
}
system("pause");

- Anonymous December 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string s;cin>>s;int i=0,l=s.length();
while(i<l)
{
int count=1;
while(i<l && s[i]==s[i+1]){count++;i++;}
cout<<count<<s[i];i++;
}
system("pause");

- sg December 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive Python version:

def rlencode(s):
    if not s:
        return None
    if len(s) == 1:
        return [[s[0],1]]
    t = rlencode(s[:-1])
    if t[-1][0] != s[-1]:
        return t + [[s[-1],1]]
    t[-1][1] += 1
    return t

def rldecode(code):
    return ''.join(i[1]*i[0] for i in code)

original = 'aaaabbbbbccd'
print('original = ' + original)
encoded = rlencode(original)
print('encoded = ' + str(encoded))
decoded = rldecode(encoded)
print('decoded = ' + decoded)

- Bullocks December 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Output looks like:

original = aaaabbbbbccd
encoded = [['a', 4], ['b', 5], ['c', 2], ['d', 1]]
decoded = aaaabbbbbccd

- Bullocks December 29, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can add an "x" and pad all digits (so that its unambigious)

mississippi --> mi2xsi2xsi2xpi
123www -->1x11x21x33xw

code:

string compress_straight(string::iterator cur, string::iterator end) {
  stringstream stream;
  while(cur != end) {
    auto next_char = find_if_not(cur, end, [&](char c) { return c == *cur; });
    int runlen = distance(cur, next_char);
    if (runlen > 1 || isdigit(*cur)) {
      stream << runlen << 'x';
    }
    stream << *cur;
    cur = next_char;
  }
  return stream.str();
}

int main() {
  string word;
  while (true) {
    getline(cin,  word);
    cout << compress_straight(word.begin(), word.end()) << "\n";
  }
}

- 6b September 12, 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