Google Interview Question


Country: United States




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

Same implementation as below in Java

public static void printAscii(){
		printAscii(0);
	}
	
	public static void printAscii(int x){
		char y = (char)x;
		System.out.println(y);
		System.out.println(x);
		x += 1;
		if(x< 129){
			
			printAscii(x);
		}

}

- roshenw January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think the range should be 0 - 127,

so x < 128

and,

we only need to print y but not x.

- JY February 02, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# implementation ( ascii 7-bit reperesentation 0- 128) here is c# code

static void WriteAllAscii()
        {
            WriteAllAscii(0);
        }

        static void WriteAllAscii(int x)
        {
            if (x == 129)
            {
                return;
            }
            Console.Write((char)x);
            WriteAllAscii(x + 1);

}

- Enkokow January 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another Java one

public static void main(String[] args) {
      printASCII(0);
   
   }
   public static void printASCII(int charNum) {
      if(charNum < 255) {
         char toPrint = (char)charNum;
         System.out.println(toPrint);
         printASCII(++charNum);
      }
   }
}

- DapperDodger January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printASCII() {
	
	asciiHelper(0);
}

public static void asciiHelper(int x) {
	
	if (x < 256) {
	
		System.out.println((char) x);
		asciiHelper(x + 1);
	}
}

- SK January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think ascii values should be in range of [0, 127]

- harry January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ASCII printable characters are from character code 32-127

void printAscii(int iAscii)
{
	
	if(iAscii>=32 && iAscii<=127)
	{
		cout << endl << char(iAscii);
		iAscii++;
		printAscii(iAscii);
	}

}

- sv January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

static const char allAsciiChars[] = {
  0x20, 0x21, ... , 0x7f, 0x0
};

void printAscii() {
  printf("%s\n", allAsciiChars);
}

No need to use recursion for something like this...

- npc January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python approach:

def solution(i=0):
    if i >= 128:
        return None
    else:
        print chr(i)
        solution(i + 1)
    pass

solution(0)

- Raymend January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use recursion.

void ascii(int i){
		printf("%d -> %c\n",i,i);
		ascii(i+1);
		if(i >= 256){
			return;
		}
	}

- subrata.das.it.kgec January 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hmmm, the question says no for or while loops, but what about do-while? :-)

#include <stdio.h>
void printAscii() {
  int i = 0x20;
  do {
    putchar(i++);
  } while (i < 0x80);
  putchar('\n');
}

- npc January 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think they meant no loops can be used.

- Prateek January 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hmmm, the question says no for or while loops, but what about do-while? :-)

#include <stdio.h>
void printAscii() {
  int i = 0x20;
  do {
    putchar(i++);
  } while (i < 0x80);
  putchar('\n');
}

- npc January 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintASCII {

    public static void main(String args[]) {
        for (int i = 0; i < 256; i++) {
            System.out.println((char) i);
        }
    }

}

- Magemello January 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ASCIICharacter {

/**
* @param args
*/
public static void main(String[] args) {
printASCII(0);

}

public static void printASCII(int number) {
if (number < 0 || number > 127) {
return;
}
System.out.println((char) number);
printASCII(++number);
}

}

- sumit January 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Use recursion
#include<iostream>
using namespace std;

void printAscii(int x){
if(x == 129)
return;
cout<< (char) x << " " ;
x = x+1;
printAscii(x);
}
int main(){
int x = 0;
printAscii(x);
return 0;
}

- Anonymous January 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't this print only chars from '!' to '~' ?

- Prateek January 31, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using recursion is valid here ?

- Prateek January 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In python:

def p(val):
        print chr(val)," ",
        if val < 122:
                p(val+1)

p(65)

- mvalero February 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another recursive

private static char recursiveAsciiPrint(int ascii) {
		if (ascii == 0 || ascii > 127) return '\0';
		System.out.println((char)ascii);
		return (char)recursiveAsciiPrint(--ascii);
	}

- DerekPK May 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hmmm, the question says no for or while loops, but what about do-while? :-)

#include <stdio.h>
void printAscii() {
  int i = 0x20;
  do {
    putchar(i++);
  } while (i < 0x80);
  putchar('\n');
}

- npc January 29, 2015 | 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