Microsoft Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
i have the same doubt ..........................
what should be the output in such cases
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;
}
}
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);
}
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);
}
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}
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}
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();
}
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);
}
what is the expected output for: AABBAA
- suraj May 13, 20121)2{A}2{B}2{A}
or
2)4{A}2{B}