Google Interview Question for SDE1s


Country: United States




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

One possible Solution using BFS:

String findSecret(String input) {
		Queue<String> queue = new ArrayBlockingQueue<String>((int)Math.pow(2, input.length()+1));
		queue.add("");

		while (true) {
			String lastText = queue.poll();
			if (isTheSecret(lastText))
				return lastText;
			if (input.length() == lastText.length())
				continue;
			String fork1 = lastText + Character.toUpperCase(input.charAt(lastText.length()));
			String fork2 = lastText + Character.toLowerCase(input.charAt(lastText.length()));
			queue.add(fork1);
			queue.add(fork2);
		}
	}

An example of how it works:

public static void main(String args[]) {
		System.out.println(findSecret("abcdefghijklm"));
	}
	
	public static boolean isTheSecret(String input) {
		return input.equals("abCdeFGhiJklM");
	}
	
	public static String findSecret(String input) {
		Queue<String> queue = new ArrayBlockingQueue<String>((int)Math.pow(2, input.length()+1));
		queue.add("");

		while (true) {
			String lastText = queue.poll();
			if (isTheSecret(lastText))
				return lastText;
			if (input.length() == lastText.length())
				continue;
			String fork1 = lastText + Character.toUpperCase(input.charAt(lastText.length()));
			String fork2 = lastText + Character.toLowerCase(input.charAt(lastText.length()));
			queue.add(fork1);
			queue.add(fork2);
		}
	}

Hope this answers your question : )

- Edwin H. January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What's the meaning of a hidden string?
If it is totally hidden, how can we check if it is matched.

- Little Lion January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One possible Solution using BFS:

String findSecret(String input) {
		Queue<String> queue = new ArrayBlockingQueue<String>((int)Math.pow(2, input.length()+1));
		queue.add("");

		while (true) {
			String lastText = queue.poll();
			if (isTheSecret(lastText))
				return lastText;
			if (input.length() == lastText.length())
				continue;
			String fork1 = lastText + Character.toUpperCase(input.charAt(lastText.length()));
			String fork2 = lastText + Character.toLowerCase(input.charAt(lastText.length()));
			queue.add(fork1);
			queue.add(fork2);
		}
	}

An example of how it works:

public static void main(String args[]) {
		System.out.println(findSecret("abcdefghijklm"));
	}
	
	public static boolean isTheSecret(String input) {
		return input.equals("abCdeFGhiJklM");
	}
	
	public static String findSecret(String input) {
		Queue<String> queue = new ArrayBlockingQueue<String>((int)Math.pow(2, input.length()+1));
		queue.add("");

		while (true) {
			String lastText = queue.poll();
			if (isTheSecret(lastText))
				return lastText;
			if (input.length() == lastText.length())
				continue;
			String fork1 = lastText + Character.toUpperCase(input.charAt(lastText.length()));
			String fork2 = lastText + Character.toLowerCase(input.charAt(lastText.length()));
			queue.add(fork1);
			queue.add(fork2);
		}
	}

Hope this answers your question : )

- Edwin H. January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just double-clicked the submit button... Sorry for that :(

- Edwin H. January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think you can just turn both to all lowercase and compare? to use extra O(2^n) time and space just for finding if the hidden string matches a give string doesn't make any sense to me.

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

Backtracking in C++.

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

void combinations(int depth, string gen, string &S) {
    if (depth == (int)S.size()){
        //check if gen matches hidden string
        cout << gen << endl;
        return;
    }

    combinations(depth + 1, gen + (char)tolower(S[depth]), S);
    combinations(depth + 1, gen + (char)toupper(S[depth]), S);
}

int main(){
    string S;
    cin >> S;
    combinations(0, "", S);
    return 0;
}

- Inucoder August 15, 2014 | 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