Interview Question

Software Development Engineer In Test (SDET) Interview Bangalore (India)

In a sequence of alphabets (like aaabbddaabbcc) write a

  program to find the number of the consecutive alphabets in and print the alphabet and number. example :if input is aaabbddaabbcc then output should be 3a, 2b, 2d, 2a, 2b, 2c
Tags:
algorithm, sequence, string
Answer

Interview Answer

3 Answers

0

I misunderstood the question, and wrote the program for printing number of a's or b's etc., present in the sequence when I showed him he asked me to code according to his requirement, the program should produce output without any errors,during this time he was writing something in the laptop. I wrote program correctly. He said ok.

Interview Candidate on May 24, 2012
0

void PrintAlphaNum( string str )
{
   if ( (str == null) || str.Length == 0 )
   {
       return;
   }

    char lastChar = str[0];
    int count = 0;

    foreach( char ch in str )
    {
        if ( ch == lastChar )
        {
            count++;
        }
        else
        {
                Console.Write("{0}{1}, ", count, lastChar );
                count = 1;
                lastChar = ch;
        }
    }
    if ( count > 0 )
    {
        Console.WriteLine("{0}{1}", count, lastChar );
    }
}

MessyHack on May 29, 2012
1

#Iinclude<stdio.h>
#include<string.h>
main()
{
   char str[ ];
  printf("Enter the input\n");
  scanf("%c",&str);
  printf("input:%c",str);
  int len= strlen(str);
  int alp[26];
 if(str!='NULL' && len!=0)
{
for(int i=0;i<len;i++)
switch(str[i])
{
case a: alp[0]++;
               break( );
case b:alp[1]++;
               break( );
case c: alp[2]++;
               break( );
.
.
.
.
case z: alp[25]++;
               break( );
}
for(int i=0;i<len;i++)
{
if(alp[i]!=0)
printf("%d %c",alp[i],str[i]);
}
}

MD UBEAD BASHA on Jun 3, 2012

Add Answers or Comments

To comment on this Question, Sign In with Facebook or Sign Up