Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

private static String camilCase(String input) {
		String output = "";
		if (input == null || input.isEmpty()) {
			return output;
		}
		for (int i = 0; i < input.length(); i++) {
			if (Character.isUpperCase(input.charAt(i)) && i > 0) {
				output += '_';
			}
			output += Character.toLowerCase(input.charAt(i));
		}
		return output;
	}

- BerMuc June 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is a solution in C

#include <stdio.h>

int camlConv(const char *in, char *out)
{
        int iCount = 0,oCount = 0;
        if (in == NULL)
        {
                out[oCount] = '\0' ;
        }

        while(in[iCount] != '\0')
        {
                if (in[iCount] <= 'Z' && in[iCount] >= 'A')
                {
                        if (oCount != 0)
                        {
                                out[oCount++] = '_';
                        }
                        out[oCount] = in[iCount] + ('a' - 'A');
                }
                else
                {
                        out[oCount] = in[iCount];
                }
                iCount++;
                oCount++;

        }
        out[oCount] = '\0' ;

        return oCount;
}

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

This solution is without any library function call. thank to power of C :)

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

Here is a possible solution in Java and JUnit

import org.junit.Test;
//
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
//
public class CamelCaseToSnakeCaseTest {
    //
    @Test
    public void testNullString() throws Exception {
        assertThat(camelToSnake(null), is(nullValue()));
    }
    //
    @Test
    public void testBlankString() throws Exception {
        assertThat(camelToSnake(" "), is(""));
    }
    //
    @Test
    public void testCamelString() throws Exception {
        assertThat(camelToSnake("SomeGoodName"), is("some_good_name"));
    }
    //
    private String camelToSnake(final String camelString) {
        // if null return null
        // that can be discussed
        if (camelString == null) {
            return camelString;
        }
        // if given a blank string return an empty one
        String trimmedCamel = camelString.trim();
        if (trimmedCamel.isEmpty()) {
            return trimmedCamel;
        }
        // a buffer for the snake case result
        StringBuilder snakeCase = new StringBuilder();
        char snakeSeparator = '_';
        // iterate over code points
        int codePointCount = trimmedCamel.codePointCount(0, trimmedCamel.length());
        for (int i = 0; i < codePointCount; i++) {
            int codePoint = trimmedCamel.codePointAt(i);
            // if the current code point is in upper case and not the first
            if (Character.isUpperCase(codePoint) && i != 0) {
                snakeCase.append(snakeSeparator);
            }
            // append lower case code point to the buffer
            snakeCase.appendCodePoint(Character.toLowerCase(codePoint));
        }
        // convert the buffer to a string
        return snakeCase.toString();
    }
//
}

- Dmytro Chyzhykov January 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

JavaScript, Ascii characters:

function work(str){
  if(!str || !str.length)
    return null;
  var output='';  
  var upperACode= 'A'.charCodeAt(0);
  var upperZCode = 'Z'.charCodeAt(0);
  for(var i=0;i<str.length;i++){
    var c = str.charCodeAt(i);
    if(c>=upperACode && c<=upperZCode){
      var prefix = i===0 ? '' : '_';
      output += (prefix + str.charAt(i).toLowerCase());
    }
    else{
      output += str.charAt(i);
    }
  }
  return output;
}

- S.Abakumoff January 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
     * time: O(N)
     * space: O(N)
     */
    private String toUnderscore( String str, char substitutionCh ){

        final int offset = (int)'a' - (int)'A';
        final int bigA = 'A';
        final int bigZ = 'Z';

        if( str == null ){
            throw new IllegalArgumentException("NULL 'str' parameter passed");
        }

        char[] bufArr = new char[ (str.length() << 1) - 1 ];

        int chCode;
        int index = 0;

        for( int i = 0; i < str.length(); i++, index++ ){

            chCode = str.charAt(i);

            if( chCode >= bigA && chCode <= bigZ ){
                if( i > 0 ){
                    bufArr[index] = substitutionCh;
                    ++index;
                }

                chCode += offset;
            }

            bufArr[index] = ( (char)chCode );
        }

        return new String(bufArr);
    }

- m@}{ January 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CamelCase {
static char capitalA='A';
static char capitalZ='Z';
public static void main(String args[]) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter the CamelCase String");
String camelCase = br.readLine();
char arr[] = camelCase.toCharArray();
String newString = "";
if(capitalA<= arr[0] && capitalZ >=arr[0]){
arr[0]=Character.toLowerCase(arr[0]);
}
newString = arr[0]+"";
for(int i=1;i< arr.length;i++){
if(capitalA<= arr[i] && capitalZ >=arr[i]){
arr[i]=Character.toLowerCase(arr[i]);
newString = newString+"_"+arr[i];
}
else {
newString=newString+arr[i];
}
}
System.out.println("new String is :"+newString);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

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

Here's what I got

public static void main(){

                try{
			String cam = CamelToSlug("AcceptThis");
			//CamelToSlug(cam);
			System.out.println(cam);
		}
		catch(IOException e){
			e.printStackTrace();
		}
}

public static String CamelToSlug(final String camCase) throws IOException{
		if(camCase == null || camCase.isEmpty()){
			throw new IOException(camCase + " empy value");
		}
		HashMap<String, Integer> slugSpot = new HashMap<String, Integer>();
		//char[] cstr = camCase.toCharArray();
		for(int i = 0; i < camCase.length(); i++){
			char check = camCase.charAt(i);
			if(check >= 'A' && check <= 'Z'){
				slugSpot.put(String.valueOf(check),i);
			}
		}
		char[] slugStr = new char[camCase.length() + slugSpot.size()-1];
		camCase.toLowerCase();
		System.out.println(slugStr.length);
		System.out.println(slugSpot.get("A"));
		for(int i = 0,j = 0; i < slugStr.length; i++){
			if(i != 0 && slugSpot.containsValue(i)){
				slugStr[i] = '_';
				System.out.println("here");
				j++;
			}
			else{
				slugStr[i] = camCase.charAt(i-j);
				if(i>=camCase.length()) j++;
			}
		}
		String returnable = String.valueOf(slugStr);
		return returnable.toLowerCase();
	}

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

Here is C# solution.

public static string ToUnderscore(string input)
{
    if (String.IsNullOrEmpty(input))
    {
        return null;
    }
   
    string result = null;
  
    for (int i = 0; i < input.Length; i++)
    {
        if ('A' <= input[i] && input[i] <= 'Z')
        {
            if (i > 0)
                result += '_';
            result += char.ToLower(input[i]);
        }
        else
        {
            result += input[i];
        }
    }
   
    return result;
}

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

Here is my solution using Perl and regular expressions:

use strict;
use Test::Simple tests => 7;

sub camel_to_underscore {
    $_ = shift;
    # Replace first capital by lowercase
    # if followed my lowercase.
    s/^([A-Z])([a-z])/lc($1).$2/e;
    # Substitute camelCase to camel_case
    s/([a-z])([A-Z])/$1.'_'.lc($2)/ge;
    return $_;
}

# Tests using Test::Simple
ok(camel_to_underscore("HelloWorld") eq "hello_world");
ok(camel_to_underscore("helloWorld") eq "hello_world");
ok(camel_to_underscore("") eq "");
ok(camel_to_underscore("hello") eq "hello");
ok(camel_to_underscore("Hello") eq "hello");
ok(camel_to_underscore("HELLO") eq "HELLO");
ok(camel_to_underscore("h e l l o") eq "h e l l o");

- jacques@supcik.net January 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's my code in python:

import sys

if len(sys.argv) == 1:
	print "No input"
	sys.exit()

inp = sys.argv[1]
output = ""
if inp[0].isupper():
	output = inp[0].lower()
else:
	output = inp[0]
for i in range(1, len(inp)):
	if inp[i].isupper():
		output += "_" + inp[i].lower()
	else:
		output += inp[i]
		
print output

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

C++

std::string careercup15139669(const std::string& text)
{
	std::string res;
	for (std::string::size_type i = 0; i != text.size(); ++i)
	{
		res.push_back(tolower(text[i]));

		if (i + 1 == text.size())
			break;
		
		if (isupper(text[i + 1]))
			res.push_back('_');
	}
	return res;

}

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

c++ code below:


#include <iostream>
#include <string>

using namespace std;

string CCtous(string input)
{
int inlength = input.length();
string output = "";
for (int ii=0; ii<inlength; ii++)
{
char iichar = input[ii];
if ((int)iichar >= 'A' && (int)iichar <= 'Z')
{
int asciival = (int)iichar;
char conv = (char)( 'a' + asciival - 'A');
if (ii!=0)
{
output = output + "_";
}
output = output + conv;
}
else
{
output = output + iichar;
}
}
return output;
}


int main()
{
string test = "NaCl";
cout<<CCtous(test)<<endl;
return 0;
}

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

#include <iostream>

using namespace std;
int main(int argc, char *argv[]) {
    int count = 0;    
    char *p = "helloWorld!!!";
    
    for (int i = 1; i < strlen(p); i++) {
        if(p[i] > 64 && p[i] < 91){
            count++;
        }
    }
    int newLength = strlen(p) + count;
    char *result = new char[newLength];
    
        for (int i = 0, j = 0; i < strlen(p); i++,j++) {
                if(p[i] > 64 && p[i] < 91){
                    if(i == 0){
                        result[0] = p[0] + 32;
                    } 
                    else {
                        result[j++] = '_';
                        result[j] = p[i] + 32;
                    }
                }
                else {
                    result[j] = p[i];
                }
        }
        for (int i = 0; i < strlen(result); i++) {
            cout << result[i];
        }
}

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

I'm unsure of some cases.

camelCase => camel_case

getX => get_x

get => get

getADog => get_a_dog <-?

getHTTPRequest => get_??_request <-?

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

private static String toUnderscoreCase(String s) {
		if (s == null || "".equals(s))
			return s;
		char[] c = s.toCharArray();
		char[] r = new char[c.length * 2];
		int j = 0;
		for (int i = 0; i < c.length; i++) {
			if (i > 0 && isUpperCase(c[i])) {
				r[j++] = '_';
			}
			r[j++] = toLowerCase(c[i]);
		}
		return new String(r, 0, j + 1);
	}

	private static char toLowerCase(char c) {
		if (isUpperCase(c)) {
			return (char) (c+32);
		}
		return c;
	}

	private static boolean isUpperCase(char c) {
		return c > 64 && c < 91;
	}

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

def convertTitle(title):
  """Convert Camilcase to underscore-lowercase.
  >>> convertTitle('')
  ''
  >>> convertTitle('Hello')
  'Hello'
  >>> convertTitle('upperCase')
  'upper_case'

  """
  newTitle = ''
  if title == '':
    return newTitle
  else:
    for ind in range(len(title)):
      if ind > 0:
        if title[ind].upper() == title[ind]:
          newTitle = newTitle + '_' + title[ind].lower()
        else:
          newTitle = newTitle + title[ind]
      else:
        newTitle = title[ind]
    return newTitle

if __name__ == '__main__':
  import doctest
  doctest.testmod()
  print(convertTitle('upperCase'))

- :) October 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def camilCaseToUnderscore(a):
    b = a.lower()
    new = ''
    idx = 0
    for i,j, in zip(a,b):
        idx+=1
        if ord(i)==ord(j) or idx==1:
            new += j
        else:
            new += '_'+j
    return new

- Zaigham November 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def cameltounderscore(name):
if not name:
return ""
Output = name[0].lower()+name[1:]
for c in Output:
if c.isupper():
Output = Output.replace(c,"_"+c.lower())
return Output


name = str(input("Enter the Name:"))
result = cameltounderscore(name)
print(result)

- Elango December 12, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def cameltounderscore(name):
if not name:
return ""
Output = name[0].lower()+name[1:]
for c in Output:
if c.isupper():
Output = Output.replace(c,"_"+c.lower())
return Output
name = str(input("Enter the Name:"))
result = cameltounderscore(name)
print(result)

- Elango December 12, 2023 | 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