Google Interview Question for Software Developers


Country: United States
Interview Type: Phone Interview




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

Not sure why "abc.pqr.google.com" and "pqr.abc.google.com" are not valid as URLs.
As far as I understand, in the first part we are looking for the number of times a particular suffix is present in the list of strings. For this, a trie should work (the code below).
And in the second part, it looks like we should find how many times an arbitrary string appears as a substring int the list of strings. Like we can query 'qr.goog'. In this case, for each input string, we can find all of the substrings it can have, and add all of those substrings into hash table: substring => count.

#include <iostream>
#include <unordered_map>

using namespace std;

class Node {
	public:
		Node() {
			count_ = 0;
		}
		~Node()
		{
			for (auto child : children_) {
				delete child.second;
			}
		}
		Node *Add(char c)
		{
			Node *n = Get(c);
			if (!n) {
				children_[c] = n = new Node();
			}
			++n->count_;
			return n;
		}
		Node *Get(char c) const
		{
			auto it = children_.find(c);
			return (it != children_.end()) ? it->second : NULL;
		}

		int count_;

	private:
		unordered_map<char, Node *> children_;
};

class Trie {
	public:
		void Add(string const &s)
		{
			Node *n = &root_;
			for (int i = s.size() - 1; i >= 0; --i) {
				n = n->Add(s[i]);
			}
		}
		int GetCount(string const &s) const
		{
			Node const *n = &root_;
			for (int i = s.size() - 1; i >= 0; --i) {
				n = n->Get(s[i]);
				if (!n) {
					return 0;
				}
			}
			return n->count_;
		}

	private:
		Node root_;
};

int main(int argvc, char const **argv)
{
	Trie trie;
	trie.Add("abc.pqr.google.com");
	trie.Add("pqr.google.com");
	trie.Add("pqr.google.net");
	trie.Add("yahoo.com");
	trie.Add("abc.yahoo.com");

	cout << trie.GetCount("google.com") << "\n";
	cout << trie.GetCount(".com") << "\n";
	cout << trie.GetCount("pqr.google.com") << "\n";

	return 0;
}

- Alex October 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How does getCount work? eg. for the query "google.com", the search in the trie is going to start with a/p/y how are you supposed to reach "g"?

- sri November 25, 2017 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void main(String[] args) {

		Trie root = new Trie("");
		add(root, "abc.pqr.google.com");
		add(root, "pqr.google.com");
		add(root, "pqr.google.net");
		add(root, "yahoo.com");
		add(root, "abc.yahoo.com");

		display(root, "");
	}

	public static void display(Trie node, String str) {

		if (node == null)
			return;
		if (node.next.isEmpty())
			return;

		List<Trie> next = node.next;
		for (Trie trie : next) {
			display(trie, str + "." +trie.word);
			System.out.println(str + "." +trie.word + " - " + trie.count);
		}
	}

	public static void add(Trie root, String str) {

		String[] arr = str.split("\\.");
		int n = arr.length;
		int k = 0;
		while (k < n) {
			Trie node = root;
			for (int i = k; i < n; i++) {
				Trie t = null;
				for (Trie tr : node.next){
					if (tr.word.equals(arr[i])) {
						t = tr;
						break;
					}
				}
				if (t == null) {
					t = new Trie(arr[i]);
					node.next.add(t);
				}
				t.count += 1;
				node = t;
			}
			k++;
		}
	}

	static class Trie {

		String word;
		List<Trie> next = new ArrayList<Trie>();
		int count;

		public Trie(String word) {
			this.word = word;
		}
	}

- sudip.innovates October 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As per my understanding, the first part can be done using a trie (since their suffix remains the same). But what about the follow-up? Is hash-table the only way to do it?

- lkjhgfdsa October 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Maybe this question is inspired by Google big table paper. But I am not sure what the question really is. Do you want all the urls ending in .com or ending in Google.com?

The question asks for substring, which could be "e.c" ... But then the fact those strings are urls doesn't matter.

Let's assume the list of URLs is huge, is updated rarely whereas there are many queries. Let's further assume the query asks for a common suffix like give me all urls ending in .com or give me all urls ending in "e.com"

In this case, we could revert the urls "com.google..." and sort them once. Then we could binary search into the sorted lists. That can be parallelized neatly (all steps).

That's basically what @Alex does, just he inverts the string when constructing the Trie and constructing the Trie is favorable in O-notation. Just keep in mind, Tries are difficult to scale for big data...

- Chris October 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Set<String> set = new HashSet<String>();
set.add("abc.pqr.google.com");
set.add("pqr.google.com");
set.add("pqr.google.net");
set.add("yahoo.com");
set.add("abc.yahoo.com");

Iterator<String> itr = set.iterator();
String[] arr = new String[100];
List<String> ar = new ArrayList<String>();
Map<String, Integer> map = new HashMap<String, Integer>();
while (itr.hasNext()) {
arr = itr.next().split("\\.");
for (int i = 0; i < arr.length; i++) {
if(map.containsKey(arr[i]))
{
map.put(arr[i], map.get(arr[i])+1);
}
else{
map.put(arr[i], 1);
}
}
}
for(Entry<String, Integer> es: map.entrySet()){
System.out.println(es.getKey() +" " + es.getValue());
}

- Deepak October 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In java:

private void method(){

Set<String> set = new HashSet<String>();
set.add("abc.pqr.google.com");
set.add("pqr.google.com");
set.add("pqr.google.net");
set.add("yahoo.com");
set.add("abc.yahoo.com");

Iterator<String> itr = set.iterator();
String[] arr = new String[100];
List<String> ar = new ArrayList<String>();
Map<String, Integer> map = new HashMap<String, Integer>();
while (itr.hasNext()) {
arr = itr.next().split("\\.");
for (int i = 0; i < arr.length; i++) {
if(map.containsKey(arr[i]))
{
map.put(arr[i], map.get(arr[i])+1);
}
else{
map.put(arr[i], 1);
}
}
}
for(Entry<String, Integer> es: map.entrySet()){
System.out.println(es.getKey() +" " + es.getValue());
}
}

- Deepak October 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi ChrisK,

Thank you for your response.
Your points are pretty good; I realized that I should have asked him if "e.c" is a valid substring as well (in this context). From his example though, I feel as if he is splitting the strings on the basis of the dots.

As for the follow-up, I think we cannot leverage the existing trie structure from step 1:

root
              /         \
          .com     .net
           /  \           \
Google  Yahoo  Google
   /  		 |		|
pqr	       abc	     pqr
  |
abc

As per my understanding, what he meant was, we would have to modify the tree if `abc` now became a parent of `pqr` (as per the follow up). This is what I could understand as per my discussion with him. That is when I thought if a hash table might be useful.

- lkjhgfdsa October 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use suffix tree!

- funcoolgeek October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I may sound pretty dumb but, can't we simply use java String.contains(String)?

I'm kinda new to this interview preperation process, so thanks for caring to answer:)

- gez November 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@sri. The Reply button doesn't work for me, for some reasons, so, replying in the main thread.
The trie is built backward. So, the search will start with m/t. For "google.com", the nodes will be m->o->c->.e ...

- Alex November 25, 2017 | 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