Yahoo Interview Question for Software Engineer / Developers






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

Note Sure:

But this is what i think it should be


printf("%c",str[0]);
i = 1;
while(str[i]){


if (ASCII (str[i]) == ASCII(str[i+1])) // note no such thing as ASCII
{
printf("%c",str[i]);
i++;
}
else
printf("\n%c",str[i++]);

}

- Anonymous June 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You should check ((ASCII(str[i+1]) - ASCII(str[i]) == 1)
Not sure though.
Please correct if wrong.

- TestDoc June 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

simple O(N^2) Algorithm is ok..but i think we can do it in O(N) ??

- Shashank June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is it O(n^2) ???? i think it's O(n)

- Anonymous June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void increasestring(char* str)
{
	char* prev = str;
	while(*str)
	{
		if(*str-*prev != str - prev)
		{
			while(prev < str)
			{
				printf("%c", *prev);
				prev++;
			}
			printf("\n");
		}
		str++;
	}

	while(prev < str)
	{
		printf("%c", *prev);
		prev++;
	}
}

- ssss June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Better solution than my previous one

void increasestring2(char* str)
{
	char prev = 0;
	while(*str)
	{
		if(prev==0 || *str-prev == 1)
			printf("%c", *str);
		else
			printf("\n%c", *str);

		prev = *str;
		str++;
	}
}

- ssss June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong solution, why you put prev as 0, doesn't make sense.

- Anonymous June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

To not put \n on first line

- ssss June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void increasestring2(char* str)
{
int i=1;
printf("%c",str[0]);
while(str[i])
{
if(str[i-1] + 1 != str[i])
printf("\n");
printf("%c",str[i]);
i++;
}
}

- Vikash Tibrewal June 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void print(char *s)
{
char ch= *s;
printf("%c",ch);
while(*s++!='\0')
{
if(ch == (*s -1))
{
printf("%c",*s);
ch = *s;
}
else
{
ch = *s;

printf("\n%c",*s);
}
}
}
O(n) time....

- anon June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void print(char *s)
{
char ch = *s;
putchar(*s);
while(*s++ != 0){
if(ch != (*s - 1))
putchar('\n');
ch = *s;
putchar(ch);
}
}

- Venkat September 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java

public static void main(String args[]){
   
        String input = "ABCPTXYUQZ";
        int thisChar, prevChar;
        boolean  firstChar = true;
        for(int i=0;i<input.length();i++)
        {
            if(firstChar)
            {
                System.out.print(input.charAt(i));
            }
            else
            {
                thisChar = input.charAt(i);
                prevChar = input.charAt(i-1);
                if(thisChar != prevChar + 1){
                    System.out.print("\n");
                }

                    System.out.print(input.charAt(i));
            }
            firstChar = false;
        }

    }

O(n)

- VK June 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Statement making firstChar flag as false (firstChar = false;) need to be placed in if condition instead of setting that flag to false each and every character. Else we can use index to check for the first character i.e. if( i == 0 )

- Pratibha C B July 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

please write the above program in c without pointer variable
and also write the program in java with simlicity

- sandy June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Printorder(char a[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(i==0)
printf("%c",a[i]);
else if(a[i]==a[i-1]+1)
printf("%c",a[i]);
else
printf("\n%c",a[i]);
}
}

- Anonymous July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void PrintSeqChar(string str)
{
if (str == null || str.Length == 0)
return;

char prv = str[0];
string line = ""+str[0];

for (int i = 1; i < str.Length; i++)
{
if (prv + 1 == str[i])
{
line += str[i];
}
else
{
Console.WriteLine(line);
line = "" + str[i];
}
prv = str[i];
}
Console.WriteLine(line);
}

- Ans July 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey32300" class="run-this">
public class consecChars {

public static void main(String[] args) {

String s = "ABCXYZDDDMOP";
int length = s.length();
for(int i = 0; i < length; i++) {
int t1 = Character.getNumericValue(s.charAt(i));
int t2 = Character.getNumericValue(s.charAt(i+1));
if(t2 == (t1 + 1)) {
System.out.print(s.charAt(i) + " " + s.charAt(i+1) + " ");
i++;
} else {
System.out.println(s.charAt(i));
}
}

}

}
</pre><pre title="CodeMonkey32300" input="yes">
</pre>

- Anonymous July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n)

- Anonymous July 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void print(const char * str){
   char * prev = NULL, * cur = str;
   while (cur){
     if(prev && *prev +1 == *cur)
        printf("%c",*cur);
     else{
        printf("\n%c",*cur);
     }
     prev++; cur++;
   }
}

- Anonymous August 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude u r checking for forward condition ie ABC
input may consist BCA
try this input ABCZYXACCD
the output is
ABC
ZYX
A
C
CD

because its consicutive not only next consicutive..

- Tarun Israni October 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude u r checking for forward condition ie ABC
input may consist BCA
try this input ABCZYXACCD
the output is
ABC
ZYX
A
C
CD

because its consicutive not only next consicutive...

- Tarun Israni October 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will definately work....
#include <stdio.h>

int main()
{
  char str[20];
  int i;
  scanf("%s",&str);
  printf("%c",str[0]);
  for(i=1;str[i]!='\0';i++)
  {
    if((str[i] == str[i-1]+1) || (str[i] == str[i-1]-1))
    {
      printf("%c",str[i]);
    }
    else
    {
      printf("\n%c",str[i]);
    }
  }
  return 0;
}

- Tarun Israni October 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string str = "ABCXYZACCDL";
int start = 0;
for(int i = 0;i < str.length();)
{
if(str[i+1] == str[i] + 1)i++;
else
{
cout<<str.substr(start,i-start+1)<<endl;
i++;
start = i;
}
}

- me December 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void foo(char *str)
{
        int i,j;
        for(i=0;str[i];++i)
        {
                j=i+1;
                while(str[j] && str[j]==str[i]+1)
                {
                        printf("%c",str[i]);
                        i++;j++;
                }
                printf("%c\n",str[i]);
        }
}

Complete code here: ideone.com/okAES

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

#include<stdio.h>
int main()
{
    char s[]="abcxyzaccd";
    char a=s[0];
    int i=1;
    
    printf("%c",a);
    while(s[i]!='\0')
    {
        if((++a)==s[i])
        {printf("%c",a);i++;}
        else
        {printf("\n%c",s[i]);a=s[i];i++;}
    }
    
    getch();
}

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

// YAHOO-CAREERCUP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>


void print(char *x)
{
if(x!=NULL)
{
printf("%c",*x);










































while(*x!='\0')
{
if(*(x+1)==*(x)+1)
{
printf("%c",*(x+1));
x++;
}
else
{
printf("\n");
printf("%c",*(x+1));
x++;
}
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
char arr[]="abcxyzaccdefxacghijklmbcd";
print(arr);
getch();
return 0;
}

- Anonymous September 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintConsecutive {
    public void print(char[] chars) {
        for (int i = 0; i < chars.length; ++i) {
            System.out.print(chars[i]);
            int j = i + 1;
            for (; j < chars.length; ++j) {
                if (chars[j - 1] + 1 == chars[j]) {
                    System.out.print(chars[j]);
                } else {
                    break;
                }
            }
            System.out.println();
            i = j - 1;
        }
    }
    
    public static void main(String[] args) {
        PrintConsecutive p = new PrintConsecutive();
        p.print("ABCXYZACCD".toCharArray());
    }
}

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

In PHP
$string="ABCXYZGHKLMRTUXY";

for($i=0;$i<strlen($string);$i++)
{
$extract=substr($string,$i,1);
$ord_value=ord($extract);
$ord_value=$ord_value+1;
$m=$i;
$extract_another=substr($string,++$m,1);
$ord_value1=ord($extract_another);
if ($ord_value==$ord_value1)
{
echo $extract;
}
if ($ord_value!=$ord_value1)
{
echo $extract,"<br/>";
}
}

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

public class Strings {
public static void main(String[] args) {
String s = "ABCXYZACCDpPqrfedtsr";

System.out.print(s.charAt(0));
for (int i =1; i<s.length();i++)
{
int ascii1= s.charAt(i-1);
int ascii2= s.charAt(i);
if(ascii1==ascii2-1 || ascii1==ascii2+1)
{
System.out.print(s.charAt(i));
}
else
{
System.out.println();
System.out.print(s.charAt(i));

}

}

}

}

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

public static String conSequence(String s)
	{
		if(s.length()<=1)
			return s;
		int ascii = s.charAt(0);
		int i = 0;
		while(i<s.length() && s.charAt(i)==ascii)
		{
			System.out.print(s.charAt(i));
			i++;
			ascii++;
		}
		System.out.println();
		return conSequence(s.substring(i,s.length()));

}

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

Here is my solution in Java. Feel free to make comments.

public static void printInSequence(String str){
		if(str != null){
			char current = '\0', nextExpected = '\0';
			StringBuilder buff = new StringBuilder();
			for(int i = 0; i < str.length(); i++){
				current = str.charAt(i);
				if(nextExpected == '\0' || nextExpected == current){
					buff.append(current);
					nextExpected = (char) (current + 1);
				} else {
					System.out.println(buff.toString());
					buff = new StringBuilder();
					buff.append(current);
					nextExpected = (char) (current + 1);
				}
			}
			System.out.println(buff.toString());
		}
	}

- Daniel September 21, 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