Directi Interview Question


Country: United States




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

#include<stdio.h>
#include<string.h>
main()
{
 int i,curr,cnt;
 char arr[100],ch;
 scanf("%s",arr);
 ch=arr[0]; cnt=curr=0;
 for(i=0;i<=strlen(arr);i++)
 {
  if(arr[i]==ch) { cnt++; }
  else
  {
   if(cnt>0)   arr[curr++]=ch;
   if(cnt>1)   arr[curr++]=cnt+48;
   cnt=1;
   ch=arr[i];
  }
 }
 if(cnt>0)   arr[curr++]=ch;
 if(cnt>1)   arr[curr++]=cnt-48;
 arr[curr-1]='\0';
 printf("%s",arr );
}

- gurumukhi October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In case the string is like
abbccc
it should print
a1b2c3 or ab2c3 ?

- Anubhav November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think at the best optimisation it should print a1b2c3 if it can be done inplace

- jindal.manik November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <cstring>

using namespace std;

void replaceDuplicate(char *a) {
  int len = strlen(a);
  int count = 1;
  int iReplace = 0;
  char lastChar = '\0';
  for (int i = 0; i <= len; i++) {
    char c = a[i];
    if (c == lastChar) {
      count ++;
    } else {
      if (count > 1) {
        a[iReplace++] = lastChar;
        a[iReplace] = 48 + count;
        count = 1;
      }
      lastChar = c;
      iReplace++;
    }
  }
  a[iReplace] = '\0';
}

int main(void) {
  char a[100];
  cin >> a;
  replaceDuplicate(a);
  cout << a;
  return 0;
}

- Paul(Zhenbo Xu) October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a[iReplace] = 48 + count;
vs
a[iReplace] = '\0';

Don't you feel that something is wrong with this?

- Selmeczy, Péter October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You mean 48 + count may be out of range of char? It would be more understandable by replacing it with '0' + count.

- Paul(Zhenbo Xu) October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I see what you mean. The modification can be converting 'count' to string and writing it back to original array.

- Paul(Zhenbo Xu) October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdlib.h"
#include "string.h"
#include "stdio.h"

int main()
{
char str[]="aaaabbbccccdd";
int length=strlen(str)-1;
int i=0,count=0,k=0;
printf("befor conversion--> %s",str);
for(k=0;k<=length;k++)
{
if(str[i]!=str[k])
{
str[i+1]=count+'0';
i=i+2;
str[i]=str[k];
count=1;
}
else
{
count++;
}

}
str[i+1]=count+'0';
str[i+2]='\0';

printf("\nafter conversion--> %s",str);
return 1;
}

- atul gupta October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
int arr[100];
void count_seq(char *p)
{
    int count =1;
    for(;*p;p++){
        if(p[1] != '\0' && (p[0] == p[1]) )
        {
            count++;

        }
        else {
            if(!(p[1]))
                printf("%c%d",p[0],count);

            else{
                printf("%c%d",p[0],count);
                count =1;
            }
        }
    }
}
int main()
{
        printf("Enter the string\n");
            gets(arr);

            count_seq(arr);
    return 0;
}

- aram October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the above code written with assumption lik "abcd" strings wont come as we will never get a case when string length will increase due to count of characters...

- atul gupta October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In place compression, I did not append the count to the character if it exists only once

public char[] InPlaceCompress(string input)
{
char[] inputInChar = input.ToCharArray();

if (inputInChar.Length == 0)
return null;

char prevCharacter = inputInChar[0];
int count = 1;
int changeIndex = 0;

for (int i = 1; i < inputInChar.Length; i++)
{
if (prevCharacter == inputInChar[i])
{
count++;
}
else
{
inputInChar[changeIndex] = prevCharacter;
changeIndex++;

if (count > 1)
{
char[] countAsChars = count.ToString().ToCharArray();

for (int j = 0; j < countAsChars.Length; j++)
{
inputInChar[changeIndex] = countAsChars[j];
changeIndex++;
}

count = 1;
}

prevCharacter = inputInChar[i];
}
}

inputInChar[changeIndex] = prevCharacter;
changeIndex++;

if (count > 1)
{
char[] countAsChars = count.ToString().ToCharArray();

for (int j = 0; j < countAsChars.Length; j++)
{
inputInChar[changeIndex] = countAsChars[j];
changeIndex++;
}
}

for (int k = changeIndex; k < inputInChar.Length; k++)
{
inputInChar[k] = '\0';
}

return inputInChar;
}

tested with some inputs

input: abbbbbbbbbbbbbbbbbbbbbbbbbbccccddddd
output: ab26c4d5

input: aaaaaabbbbbccccd
output: a6b5c4d

input: abcd
output: abcd

- siva October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//void worldcount(char* );
void main()
{
char str[19];
//="aabbbcccc";
int i,len, count=1;
char temp;

printf("enter the string");
gets(str);
len=strlen(str);
for (i=0;i<len;i++)
{
temp=str[i];
if (temp==str[i+1])
count++;
else
{
printf("%c%d",str[i],count);
count=1;
}

}
printf("\n");
}

- chandu October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is this possible to increase array length for output? Because in case of abcd output should a1b1c1d1 which is bigger than the input.

- jenish.shah October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void countChars(char * inString){
 if(inString == NULL)
  return;
 int start = 0;
 int index = 0;
 int insert = 0;
 int count = 0;
 while(inString[index] != NULL){
  while(inString[index] == inString[start]){
   index++;
  }
  inString[insert] = inString[start];
  insert++;
  count = index - start;
  if(count > 1){
   insert += sprintf(inString[insert], "%d", count);
  }
  index++;
  start = index;
 }
 if(insert != 0 && insert < strlen(inString))
  inString[insert] = NULL;
}

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

#include<cstdio>
#include<cstring>
#include<cstdlib>
 
int main()
{
    char a[100];
    scanf("%s",a);
    
    int i,t,len = strlen(a);
    for(i=1,t=0;i<len;i++)
    {
                          if(a[i] != a[t])
                          {
                                  if(i-t > 1)
                                  {
                                         a[i-1] = i-t;
                                         if(a[i-1] > 9)
                                         {
                                                   a[i-2] = 127;
                                         }
                                  }
                                  
                                  t = i;
                          }
    }
    
    if(t==0)
    {
            if(len<10)
            {
                     a[1] = len + 48;
                     a[2] = 0;
            }
            else
            {
                a[1] = len/10 + 48;
                a[2] = len%10 + 48;
                a[3] = 0;
            }
            
            printf("%s",a);
            exit(0);
    }
    else
    {
        if(len-t>1)
        {
                   a[len-1] = len - t;
                   if(len - t > 9)
                   {
                          a[len-2] = 127;
                   }
        }
    }
    
    int c;
    for(i=0,c=0;i<len;i++)
    {
               if(a[i] < 10)
               {
                       c = c+ a[i] - 2;
               }
               else if(a[i] == 127)
               {
                    c = c + a[i+1] - 3;
               }
               
               a[i-c] = a[i];
    }
    int nlen = len - c;
    a[nlen] = 0;
    
    for(i=0;i<nlen;i++)
    {
                       if(a[i] < 10 )
                       {
                               a[i] = a[i] + 48;
                       }
                       else if(a[i] == 127)
                       {
                            a[i] = a[i+1]/10 + 48;
                            a[i+1] = a[i+1]%10 + 48;
                       }
    }
    
    
    for(i=0,c=0;i<nlen-1;i++)
    {
                       if(a[i]>96 && a[i+1] > 96)
                       {
                                c++;
                       }
    }
    if(a[nlen-1] > 96)
    {
                 c++;
    }
    int offset = c;
    if(len<nlen+c || c==0)
    {
                  printf("%s",a);
                  exit(0);
    }
    
    if(a[nlen-1] > 96)
    {
                 a[nlen+c-1]=49;
                 c--;
                 a[nlen+c-1] = a[nlen-1];
    }

    for(i=1;i<nlen;i++)
    {
                          if(a[nlen-1-i] > 96 && a[nlen-i] > 96)
                          {
                                   a[nlen+c-1 - i] = 49;
                                   c--;
                          }
                          a[nlen+c-1 - i] = a[nlen-1 - i];
    }
    a[nlen+offset] = 0;
    printf("%s",a);

}

As far as I can think this will work, absolutely Inspace. If it doesnt work for some testcases give me those cases. Thank You in Advance

- jindal.manik October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//program to replace char with their count

#include<iostream>
#include<cstring>

#define SIZE 100

using namespace std;


int main()
{
	char a[SIZE];
	cin>>a;

	int pos=0,count=1,i=0;

	for(int k=1;a[k]!='\0';k++)
	{
		if(a[k]==a[i])
		{
			count++;
		}
		else
		{
			a[pos] = a[i];
			if(count>9)
			{
				a[++pos]='0'+count/10;
				count = count%10;
			}
			a[++pos]='0'+count;
			pos++;
			i=k;
			count=1;
		}
	}

	a[pos]=a[i];
	if(count>9)
	{
		a[++pos]='0'+count/10;
		count = count%10;
	}
	a[++pos] = '0'+count;
	a[++pos] = '\0';

	cout<<a;

	return 0;
}

- RG October 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

s=raw_input()
l=[]
n=1
c=s[0]
for i in s[1:]:
	if i != c:
		l.append(c)
		l.append(str(n))
		c=i;
		n=0
	n+=1
	print l
l.append(c)
l.append(str(n))	

print "".join(l)

- harshit.knit November 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

char a[10] = "aabbccc";

int main()
{
char ch;
int read = 0, write = 0;
int curr_count = 0;

a[strlen(a)] = '\0';

while(a[read] != '\0' ) {
ch = a[read];
a[write] = ch;
curr_count = 1;
read++;
while(a[read] == ch) {
curr_count++;
read++;
}
a[++write] = (int) ('0' + curr_count);
printf("a = %s, ch = %c ; a[read] = %c ; a[write] = %c \n", a, ch, a[read], a[write]);
write++;
}


a[write] = '\0';


printf("%s \n", a);

return 1;
}

- Anonymous October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A few comments:
1. Why don't you write a function to do it a make main a test-bed for it?
2. Why main is returning 1??? What is the return value of main supposed to be? (It is left to the reader to answer this question)
3. Why don't you handle/test for misformed texts? E.g. "alma"? This cannot be converted inspace.
4. Your code does not handle the case when a character appears more than 9 times.

- Selmeczy, Péter October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is this O(n)?

- REAMS.AL October 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is the string abcdef... handled?

- srikantaggarwal October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The above code won't work when any character appears only once.....
for example when string is "abc" or "aab"...

- Akhilesh Pandey October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{char* str="aabbbcccaabbccc";
int* ptr=(int*) calloc(256,sizeof(int));
ptr[*str]++;
for(int i=1;i<strlen(str);i++)
{ptr[*(str+i)]++;

if(*(str+i)!=*(str+i-1))
{cout<<*(str+i-1)<<ptr[*(str+i-1)];
ptr[*(str+i-1)]=0;
}

}
if(ptr[*(str+strlen(str)-1)]==1)
cout<<*(str+strlen(str)-1)<<ptr[*(str+strlen(str)-1)];

delete ptr;
getch();
return 0;}



//Output: a2b3c3a2b2c3

other test cases: input str=aabbbcccaabbc
output=a2b3c3a2b2c1

- vishal18593 October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

has to be done in space !

- jindal.manik October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup its not inplace

- anshu.iiita.10 July 15, 2013 | Flag
Comment hidden because of low score. Click to expand.


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