Google Interview Question for Senior Software Development Engineers


Team: TEZ
Country: India
Interview Type: Phone Interview




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

public class ChangeNumberBase {

	public static String base(int number, int base) {
		String output = "";
		if (number == 0) {
			output = "0";
		} else {
			boolean negative = false;
			if (number < 0) {
				negative = true;
				number *= -1;
			}
			while (number > 0) {
				output = (number % base) + output;
				number = number / base;
			}
			if (negative)
				output = "-" + output;
		}
		return output;
	}

	public static void main(String args[]) {
		System.out.println(base(0, 3));
		System.out.println(base(3, 3));
		System.out.println(base(5, 3));
		System.out.println(base(-5, 3));
		System.out.println(base(9, 3));
		System.out.println(base(3, 4));
		System.out.println(base(4, 4));
		System.out.println(base(5, 4));
		System.out.println(base(15, 4));
	}
}

- Viv February 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class NumberSystemBase3 
{
	// return as a String to avoid overflow issues
	public String getBase3(long input)
	{
		if(input >= -2 && input < 3)  // -2, -1, 0, 1, 2
			return String.valueOf(input);
		else
		{
			char sign = ' ';
			long x = input;
			int digit;
			if(input < 0)
			{
				sign = '-';
				x = -x;
			}
			
			StringBuilder result = new StringBuilder();
			
			// start extracting the digits
			while(x > 0)
			{
				digit = (int) (x % 3);
				x = x / 3;
				result = result.append(digit);
			}
			
			if(sign == '-')
			{
				result = result.append(sign);
			}
			
			result = result.reverse();
			
			System.out.println(input + " == " + result);
			
			return result.toString();
		}
	}
	
	public long convertBase3ToInteger(String input)
	{
		if(input == null || input.length() == 0)
			return 0;
		else
		{
			String s = input.trim();
			int N = s.length();
			long result = 0;
			long multiplier = 1;
			
			int i;
			char currentCharacter;
			
			i = 0;
			if(s.charAt(0) == '-')
			{
				multiplier = -1;
				++i;
			}
			
			for(; i < N; i++)
			{
				currentCharacter = s.charAt(i);
				if(currentCharacter < '0' || currentCharacter > '2')
				{
					throw new RuntimeException("Invalid input !");
				}
				
				result = (3 * result) + (currentCharacter - '0');
			}
			
			if(multiplier == -1)
			{
				result = -result;
			}
			
			System.out.println(input + " == " + result);
			
			return result;
		}
	}
}

- Interviewer February 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def convert(i, b):
    t.append(i % 3)
    while (i > b - 1):
        i = i // 3
        t.append(i % 3)

t = []
convert(3, 3)
print(t)

- schnurk February 10, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::stoi;
using std::to_string;

// Assume:
//   denary numbers aren't negative
//   bases aren't negative
string StrFromDenaryToBase(int d, unsigned int b) {
  if (b < 2 || d < 0) {
    return "";
  }

  if (d == 0) {
    return "0";
  }

  string s; 
  while (d > 0) {
     auto x = d % b;
     d /= b;
     s.insert(0, to_string(x));
  }

  return s;
}

int main(int argc, char **argv) {
  cout << StrFromDenaryToBase(stoi(argv[1]), stoi(argv[2])) << "\n";

  return 0;
}

- thelobster February 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::stoi;
using std::to_string;

// Assume:
//   denary numbers aren't negative
//   bases aren't negative
string StrFromDenaryToBase(int d, unsigned int b) {
  if (b < 2 || d < 0) {
    return "";
  }

  if (d == 0) {
    return "0";
  }

  string s; 
  while (d > 0) {
     auto x = d % b;
     d /= b;
     s.insert(0, to_string(x));
  }

  return s;
}

int main(int argc, char **argv) {
  cout << StrFromDenaryToBase(stoi(argv[1]), stoi(argv[2])) << "\n";

  return 0;
}

- thelobster February 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ChangeNumberBase {

public static String base(int number, int base) {
String output = "";
if (number == 0) {
output = "0";
} else {
boolean negative = false;
if (number < 0) {
negative = true;
number *= -1;
}
while (number > 0) {
output = (number % base) + output;
number = number / base;
}
if (negative)
output = "-" + output;
}
return output;
}

public static void main(String args[]) {
System.out.println(base(0, 3));
System.out.println(base(3, 3));
System.out.println(base(5, 3));
System.out.println(base(-5, 3));
System.out.println(base(9, 3));
System.out.println(base(3, 4));
System.out.println(base(4, 4));
System.out.println(base(5, 4));
System.out.println(base(15, 4));
}
}

- Viv February 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BigInteger function(int num) {
        if (num == 0)
            return 0

        Stack<String> stackReminders = new Stack<>()
        StringBuilder res = new StringBuilder()
        boolean negative = num < 0 ? true : false
        int element = negative ? num * -1 : num

        while (element != 0) {
            int newElement = element / 3
            stackReminders.push((element - newElement * 3).toString())
            element = newElement
        }

        while (!stackReminders.isEmpty()) {
            res.append(stackReminders.pop())
        }

        negative ? res.toBigInteger() * -1 : res.toBigInteger()
    }

- Natalia March 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String toBase3(int input) {
		int pow3 = 1;
		while (Math.pow(3, pow3) < input) {
			++pow3;
		}
		
		final StringBuilder sb = new StringBuilder();
		
		for (int i = pow3; i > 0; --i) {
			for (int j = 2; j >= 0; --j) {
				int digit = Math.pow(3, i-1) * j;
				if (pow <= input) {
					sb.append(j);
					input -= digit;
					break;
				}
			}
		}
		return sb.toString();
	}

- jtcgen April 02, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String getBase3(int num) {
        if (num == 0) return "0";
        StringBuilder str = new StringBuilder();
        boolean isNegative = false;
        if (num < 0) {
            num = -num;
            isNegative = true;
        }
        while (num != 0) {
            str.append(num%3);
            num /= 3;
        }
        if (isNegative) str.append("-");
        str.reverse();
        return str.toString();
}

- Aim_Google April 02, 2018 | 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