Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

what is the expected output for: AABBAA
1)2{A}2{B}2{A}
or
2)4{A}2{B}

- suraj May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I also want to know

- milo May 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It looks like text file compression algorithm . so it should be 2{A}2{b}2{A}.

- Sagar May 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i have the same doubt ..........................

what should be the output in such cases

- Manish May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i have the same doubt ..........................

what should be the output in such cases

- Manish May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char CurrChar = str[0];
int CurrCount = 0, i, j = 0;
char OutStr[100];
for (i = 0; CurrChar != 0,i < strlen(str); i++)
{
if(CurrChar == str[i]) CurrCount++;
else{
j = InttoStr(CurrCount, OutStr, j);
OutStr[j++] = '{';
OutStr[j++] = CurrChar;
Outstr[j++] = '}';
CurrChar = str[i];
CharCount = 1;
}
}

- krishna May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It looks like text file compression algorithm . so it should be 2{A}2{b}2{A}.

- Sagar May 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

String pattern= new String("AAABBGFF";
int count = 1;
char prev = pattern.charAt(0);

for(int t=1;i<pattern.length;i++)
{
  char current = pattern.charAt(i);
  if( current!=prev)
  {
    System.out.print(count+"{"+prev+"}");
    count = 1;
  }
 else
 {
    count++;
  }
prev=current;
}

- Pavan Dittakavi May 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
int main()
{
        int i = 0;
        int count = 0;
        char a[]="AAABBGFF";
        while(i<strlen(a))
        {
                while(a[i] == a[i+1])
                {
                        i++;
                        count++;
                }
                count++;
                printf("%d{%c}",count,a[i]);
                count = 0;
                i++;
        }
}

- Ramachandran May 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void formatString() {
		// TODO Auto-generated method stub
		
		String s= "AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",temp="";
		
		
		
		char[] a = s.toCharArray();
		int count=0;
		
		for (int i = 0; i < a.length-1; i++) {
			
		
			
			if(a[i]==a[i+1]){
				
				count++;
				
				if(i==a.length-2){
					count++;
					temp=temp+count+"{"+a[i]+"}";
				}
				
				
			}else{
				count++;
				temp=temp+count+"{"+a[i]+"}";
				count=0;
			}
			
		}
		
		
		System.out.println("=========="+temp);

}

- suvrokroy May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is called RLE. The question is how would you want to have DDDDDDDDDDDDDDDD represented? as FD or 16D. Also, if the number of D's is say 1024, how would you address this. Basically in the answer, how to identify if the char is a part of the length or the char itself.

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

Assumption: Input is only characters A-Z, a-z

typedef struct _CharCount
{
    char chr;
    int count;
}CharCount;

main()
{
    CharCount   stCharCount[52] = {0};
    char *temp = "AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv";
    char chr = *temp;
    int i = 0;
    while(*temp != '\0')
    {
        if(chr == *temp)
        {
            stCharCount[i].chr = *temp;
            stCharCount[i].count++;
        }
        else
        {
            printf("%d{%c}",stCharCount[i].count,stCharCount[i].chr);
            i++;
            stCharCount[i].chr = *temp;
            stCharCount[i].count++;
            chr = *temp;
        }
        temp++;
    }
    printf("%d{%c}",stCharCount[i].count,stCharCount[i].chr);
}

- Ravindian May 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.careercup;

/**
 * Given AAABBGFF output 3{A}2{B}1{G}2{F}
 * 
 * @author root
 * 
 */
public class TextCompression {

    public static void main(String args[]) {
        System.out.println("AAABBGFF:" + compressedString("AAABBGFF"));
        System.out.println("AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv:"
                + compressedString("AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv"));
        System.out.println("AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD:"
                + compressedString("AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"));
    }

    static String compressedString(String input) {
        StringBuffer output = new StringBuffer();
        int count = 0;
        for (int i = 0; i < input.length(); i++) {
            char curChar = input.charAt(i);
            char nextChar = 1;
            if (i + 1 < input.length()) {
                nextChar = input.charAt(i + 1);
            }
            if (curChar != nextChar) {
                output.append(count + 1);
                output.append("{");
                output.append(curChar);
                output.append("}");
                count = 0;
            } else {
                count++;
            }
        }
        return output.toString();
    }
}

Output
======

AAABBGFF:3{A}2{B}1{G}2{F}
AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv:3{A}2{B}8{G}2{F}6{X}5{T}6{y}5{v}
AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD:4{A}2{B}7{C}2{B}4{C}38{D}

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

package com.careercup;

/**
 * Given AAABBGFF output 3{A}2{B}1{G}2{F}
 * 
 * @author root
 * 
 */
public class TextCompression {

    public static void main(String args[]) {
        System.out.println("AAABBGFF:" + compressedString("AAABBGFF"));
        System.out.println("AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv:" + compressedString("AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv"));
        System.out.println("AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD:" + compressedString("AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"));
    }
    
    static String compressedString(String input) {
        StringBuffer output = new StringBuffer();
        int count = 0;
        for (int i = 0; i < input.length(); i++) {
            char curChar = input.charAt(i);
            char nextChar = 1;
            if (i + 1 < input.length()) {
                nextChar = input.charAt(i + 1);
            }
            if (curChar != nextChar) {
                output.append(count + 1);
                output.append("{");
                output.append(curChar);
                output.append("}");
                count = 0;
            } else {
                count++;
            }
        }
        return output.toString();
    }
}

Output
=====
AAABBGFF:3{A}2{B}1{G}2{F}
AAABBGGGGGGGGFFXXXXXXTTTTTyyyyyyvvvvv:3{A}2{B}8{G}2{F}6{X}5{T}6{y}5{v}
AAAABBCCCCCCCBBCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD:4{A}2{B}7{C}2{B}4{C}38{D}

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

c#
===

string displayCountCharacter(string input)
{
Dictionary<char,int> alphabets = new Dictionary<char,int>();
foreach(char c in input.ToCharArray())
{
if(alphabets.ContainKey(c))
{
alphabets[c] += 1;
}
else
{
alphabets.Add(c,1);
}
}

StringBuilder output = new StringBuilder();
foreach(KeyValuePair<char,int> item in alphabets)
{
output.Append(item.Value.ToString()+"{"+item.Key+"}");
}
return output.ToString();
}

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

initialize the values, start from 2nd character till the last and compare with the current Char.

private static void PrintFormatted(char[] inp)
        {
            int c = 1;
            char m = inp[0];
            for (int i = 1; i < inp.Length; i++)
            {

                if (m == inp[i])
                {
                    c++;
                }
                else
                {
                    Console.WriteLine("{0},{1}:", c, m);
                    c = 1;
                    m = inp[i ];
                }

            }
            Console.WriteLine("{0},{1}:", c, m);
        }

- sundara.dinakar June 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream.h>

int main()
{
    char str[30];
    gets(str);
    char lastchar=str[0];
    int count=1;
    for (int i=1;i<15;i++)
    {
        if (str[i]==lastchar)
           count++;
        else
        {
            cout<<endl<<count<<lastchar;
            count=1;
            lastchar=str[i];
        }
    }
    cin>>count;
    return 0;   
}

- Sachin June 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String Demo(String s)
{
char temp = s.charAt(0);
String g = "";
int count=1;
for(int i=1;i<=s.length-1;i++)
{
if(s.charAt(i)!=temp)
{
g = g + count+"{"+temp+"}";
count = 1;
temp = charAt(i);
}
else
count++;
}
return g;
}

- Sai Phaneendra Vadapalli June 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should we use the Huffman Encoding Compression Algo here ?

- Angshuman June 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PrintCharCount(char* s)
	{
		char* c = s;

		for(; *s; s++)
		{
			if (*s != *c)
			{
				cout << s-c << *c << " ";
				c = s;
			}
		}
		
		cout << s-c << *c << " ";
	}

- Anonymous June 12, 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