Yahoo Interview Question 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n)

- Anonymous on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on November 20, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through 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