Phone Interview with G

Please use this Google doc to code during your interview. To make hands free coding a little easier, we recommend that you use a headset or a phone with speaker option.

Best,
Google Staffing

 

//This is a test

public static void main (String [] args) {

   System.out.println(“Hello World!”);

}

“I        have         a        dog.”  ⇒ “I have a dog.”

Collapse whitespace in char array in place

public void collapseSpace(char[] s) {

   int p = 0;

   boolean firstSpace = true;

   for (int i = 0; i < s.length; i++) {

       if ((s[i] == “ “ && firstSpace) || (s[i] != “ “)) {

           copy(s, p++, i);

           firstSpace = (s[i] != “ “);

       }

   }

}

 

public void copy(char[] s, int a, int b) {

   if (a == b)

       return;

   s[a] = s[b];

}

String calc(String a, String op, String b) { }

calc(“2”, “+”, “3”) ⇒ “5”

calc(“2.0”, “+”, “3”) ⇒ “5.0”

calc(“2”, “+”, “3”) ⇒ “5”

calc(“2.0”, “+”, “3”) ⇒ “5.0”

calc(“2”, “+”, “3.0) => “5.0”

calc(“2”, “-”, “3”) ⇒ “-1”

calc(“2.0”, “-”, “3”) ⇒ “-1.0”

calc(“2”, “-”, “3.0) => “-1.0”

calc(“2”, “*”, “3”) ⇒ “6”

calc(“2.0”, “*”, “3”) ⇒ “6.0”

calc(“2”, “*”, “3.0”) => “6.0”

calc(“2”, “/”, “3”) ⇒ “0”

calc(“2.0”, “/”, “3”) ⇒ “0.7”

calc(“2”, “/”, “3.0”) => “0.7”

//check the negative operators

calc(“2”, “/”, “0”) ⇒ “N/A”
//overflow

//divide by very small numbers

//illegal input

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s