Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

Let str[] denote the given string. I am creating an array arr[] which stores the words as I read them.
i=0,k=0,count=0
while(str[i++]!='\0')
do
if(str[i]!=' ')
arr[k++]=str[i]
count++
else
if(count==1)
arr is one of the ans and continue finding for others
k=0
empty arr again

- alex December 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The question is : are we looking for unique words of 1 length? or not?

Assume we have the answer and we are taking care of this in every approach.

First Approach:
- Split by " " String[] token = input.split(" ") , go word by word and if it is of length 1 count++;
// it uses O(nr of words) additional space

SecondApproach:
- go element by element and if it is surround it by " " or if it is at the beginning of the string and followed by " " or it at the end of a string and preceded by " "
- if so count++;
// this one uses O(1) space

Third approach:
-use pattern matching:

Pattern p = Pattern.compile("\\w+");
  Matcher m = p.matcher(input);
  while(m.find()){
     if(m.group().length()==1) count++;
  }

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

public int singleAlphebetCounter(String s)
{
int counter = 0;

for(String temp:s.split(" "))
if(temp.length()==1)
counter++;

return counter;
}

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

assign each space char as operation +
each char as a value = 1
we need to convert the string into a numerical expression where a multiple char numerical value = 0
When a multiple characters string is found, its value would be str[0]-str[1], ignoring remaining characters in building expression
So the string "I am a king" will become
1+(1-1)+1+(1-1) = 2 //replace space with + and while scanning a multichar string, just scan first two chars
Complexity = O(n)

- kuldeep.hbti December 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My Algorithm as following:
1) increase the count if the character is between A-Z or a-z (we can expand the list as needed).
2) for the rest characters, reset the counter.
3) Before reset the counter, we check and see if counter == 1

while (str != 0 && str[pos] != '\0')
   {      
      if ((str[pos] >= 'A' && str[pos] <= 'Z') || (str[pos] >='a' && str[pos] <= 'z'))
      {
        counter++;
      }
      else
      {
         if (counter == 1)
         {
            printf("found:%c\n", str[pos-1]);
         }  
            
         counter = 0;
      }   
      pos++;         
   }

- geoleo December 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (isspace(*(a+1)))
{
count++;
}

if (isspace(*(a+length-2)))
{
count++;
}

for (i=1;*a;i++)
{
if (isspace(*(a-1)) && isspace(*(a+1)))
{
count++;
}
a++;
}

- Srujan December 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this??

int main()
{
    char str[]="i am a king";
    char *str2=strtok(str," ");
    
    int count=0;
    
    while(str2!=NULL)
    {
                    if(strlen(str2)==1)
                                      count++;
                    str2=strtok(NULL," ");
    }
    
    cout<<"count is"<<count;
    
    getch();
    return 1;
}

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

int main()
{
char *str = "I am a M a c";
char *tmp_str1 = NULL;
int count = 0;

tmp_str1 = str;

while (tmp_str1)
{
if ((tmp_str1[1] == '\0') || (tmp_str1[1] == ' '))
{
count++;
}
tmp_str1 = strchr (tmp_str1,' ');
if (tmp_str1)
{
tmp_str1++;
}
else
{
break;
}
}

printf ("%d", count);

}

- manish.nsit2002 December 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int alpha(char *a)
{
int count=0;
if(*(a+1)==' ');
count++;
while(*a)
{
if(*(a-1)==' '&&*(a+1)=='\0')
count++;
if(*(a+1)==' '&&*(a-1)==' ')
count++;
a++;
}
return count;

}
int main()
{
char a[20];
gets(a);
cout<<alpha(a);

getch();
return 0;
}

- XYZ January 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But here when i am entering "there is a tree" its showing 2 ....but it should show 1 becoz here only "a " is 1 alphabet

- Sourav January 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countSingleLetterWord(const char *in)
{
int count,i;
i=0;count=0;
if(*in=='\0')
{
return 0;
}
do
{
if(*in==' ' || *in=='\0')
{
if(i==1)
{
count++;
}
i=0;
}
else
{
i++;
}
}while(*in++);
return count;
}

- Anonymous January 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>

int main()
{
char arr[20]="Am i a king?";
int i=0,k=0,count=0;

while(i<strlen(arr))
{
if( arr[i]==' ')
{
if((i-k)==1)
{
count+=1;
}
k=i+1;
}
i++;
}
printf("\Number of single letter words in a sentence:%d \n ",count);
return 0;
}

- vamsha January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>

int main()
{
char arr[50]="i am a MAC ? a HOST";
int i=0,k=0,count=0;

while(i<strlen(arr))
{
if( arr[i]==' ')
{
printf("\n %d %c",i,arr[i-1]);
if( (65<=arr[i-1]&&arr[i-1]<=90) || ( 97<=arr[i-1] && arr[i-1]<=122 ) )
{
printf(" \t HI ");
printf("%d %d \n",i,k);
if((i-k)==1)
{
count+=1;
}
k=i+1;
}
else
{
k=i+1;
}
}
i++;
}
printf("\n Number of single letter words in a sentence:%d \n ",count);
return 0;
}

- vamsha January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

printf("\n %d %c",i,arr[i-1]);
printf(" \t HI ");
printf("%d %d \n",i,k);

please remove... this i used to debug my program

- Anonymous January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My implementation:

#include <stdio.h>
unsigned int countsingleword(char*p)
{
        unsigned int count, count1;
        if(NULL==p)
                return 0;
        for(count=0, count1=0;*p;p++)
        {
                if(' '==*p)
                {
                        if(1==count1)
                                count++;
                        count1=0;
                }else
                {
                        count1++;
                }
        }
        if(1==count1)
                count++;
        return count;
}

int main()
{
        char p[]="I am a humble human! I am ";
        printf("string %s has %d single word\n", p, countsingleword(p));
}

- chenlc626 February 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int no_single_letter_words(char str[])
{
int length=0,check,count=0,i=0;
while(i<strlen(str))
{
if(str[i]!=' ')
length++;
else
{
check=length;
length=0;
if(check==1)
count++;
}
i++;
}
if(length==1)count++; //cheking if the last word consists of a single letter
return count;
}
int main()
{
char str[]="I am a King";
int answer=no_single_letter_words(str);
printf("%2d\n",answer);
}

- coolanand June 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char *p="I am a king";
int n=0,i;
for(i=0; i<strlen(p);i++)
{
if((i==1) || (i==(strlen(p)-2)))
if(p[i]==' ')
n++;

if((p[i]==' ') && (p[i+2]==' '))
n++;
}

printf(" Number of single letter word in the sentence is %d \n", n);

}

- Pallavi July 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about double space in case we will have , Pallavi ?
Like : char *p="I am king r t";

- RickRoss$$ August 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* This is not a compact code ... Just an approach */
#include<stdio.h>
void wordcount(char *str)
{
	char *temp = str;
	int num =0,count = 0;
    while(*temp!= '\0')
	{
		if(*temp == ' ' && num == 1) 
		{
			count++;
			num = 0;
		}
		else
		{
		num++;
	
		}
		if(*temp == ' ' )

		{
			num = 0;
		}
			temp++;


	}
			printf("the count is %d",count);

}
int main()
{
char *str = "I am a king";
wordcount(str);
return 0;
}

- Sakthivel Sethu ,Software Engineer ,Bangalore October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{
	char c[] = "t t h to the j enfo j o k";
	char *cp = c;
	int count = 0;
	if(c[1] == ' ')/*checks 1st character is single*/
		count++;
	while((*cp) != '\0') {
		if((*cp) == ' ') {
			if( *(cp+= 2) == ' ') {
				count++;
				cp-=2;/* i used to modify the pointer, so it missed some blanks*/
			}
		}
		cp++;
	}
	if(c[strlen(c) - 2] == ' ')/*checks last character is single*/
		count++;
	printf("count = %d\n",count);
	return 0;
}

- agmegharaj November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int FindSingleWord(const char* s)
{
	int size=strlen(s);
	int count=0;
	int wordsum=0;
	if(s==NULL)
		return 0;
	for(int i=0;i<size;i++)
	{
		if(*(s+i)==' ')
		{
			if(wordsum==1)
				count++;
		    wordsum=0;
		}
		else
		{
			wordsum++;
		}
      
	}
	return count;
}

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

#include <stdio.h>

int main() {
  char *s = "I am A king";
  int count = 0;
  int base = 0;
  int iter = 0;
  while (*s != '\0') {
    if (*s == ' ') {
       if ((iter - base) == 1) {
          count ++;
       }
       base = iter+1;
    }
    iter ++;
    s ++ ;
  }

  printf("%d\n", count);
}

- vsagar June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it doesnt work if string is "I" - it shouldnt work in that case rt?

- vsagar June 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find1s(char* s)
{
	bool before=1;
	int size =strlen(s);
	int cnt=0;
	for(int i=0;i<size;i++)
	{
		if(*(s+i)!=' ')
		{
			if((*(s+i+1)==' ')||(*(s+i+1)==0))
			{
				if(before==1)
				{
				cnt++;
				i=i+1;
				}
				else
				{
					
				}
			}
			else
			{
				before=0;
			}
		}
		else
		{
			before=1;
		}
	}
	return cnt;

}

- Charles August 02, 2014 | 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