Google Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

Stlll O(n^2). Greedy fashion might make it a bit faster.

#include <iostream>
#include <string>

using namespace std;

bool isWordLegal(string s) //test function assuming only "hello" is in the dict.
{
  if (s == "hello")
    return true;
  else 
    return false;
}


int main() {
  
  string s = "abcdefghelloijklmnop";  //test target string
  
  for(int i = s.length(); i > 0; i--) //i: len of the substring, start with the full string
  {
    for(int j = 0; i + j <= s.length(); j ++)  //j: start of the substring
    {
      string test = s.substr(j, i);
      if(isWordLegal(test))
      {
        cout<<"Longest legal string: "<<test;
        break;
      }
    }
  }
  return 0;
}

- cdhsiung November 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Just detailing what you have done
May be we can validate based on the size of legal string
For example ::
size of given string is n.
Assume Legal string size is n -- only one possible
n-1 -- 2 possible
n-2 -- 3 ..
.
.
if we find the legal string we can stop and say it is the largest because we do not have to look for smaller strings.
Worst case would be when we do not find the string :O(n^2) but average case would be lot faster.

- raghu.aok November 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does not work for the string - "ahello"

- Seeker November 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Probably using the built in comparison function for Strings may resolve the issue with "ahello" as our test String. So having the method as:

bool isWordLegal(std::string word)
{
	if(word.compare("hello")) return true;
	return false;
}

- bonapatre November 17, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This would fail when you have a string like: "abchealloxyz", although this still contains "hello".

- Ahmad November 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the string "abchealloxyz", does not contain "hello".

- zr.roman February 06, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the solution has complexity O(n^3), because "substr" subroutine has linear time.

- zr.roman February 06, 2016 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Unless we actually know the legal words and are only given a method to make a boolean check we cant do better than n^2. It seems like something was omitted from this question.

- Anonymous November 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The program does not correctly find the longest legal word for this string - "ahello". It looks like this piece of code is not considering all possible combinations of the test string.

- Seeker November 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the DP solution using memorized Map with recursion. Assuming we given a dictionary of words.

public class SegmentString {
Map<String, String> memorized = new HashMap<String, String>();
public String segmentString(String input, Set<String> dictonary){
if(dictonary.contains(input))return input;
if(memorized.get(input) != null){
return memorized.get(input);
}
int length = input.length();
for(int i = 1; i < length; i++){
String prefix = input.substring(0,i);
if(dictonary.contains(prefix)){
String suffix = input.substring(i, length);
String segmentString = segmentString(suffix, dictonary);
if(segmentString!=null){
memorized.put(input, prefix + " " + segmentString);
return prefix+" "+segmentString;
}
}
}
memorized.put(input, null);
return null;
}


public static void main(String[] args){
SegmentString seg = new SegmentString();
Set<String> dictonary = new HashSet<String>();
dictonary.add("hello");
dictonary.add("ate");
dictonary.add("a");
dictonary.add("an");
dictonary.add("ant");
dictonary.add("i");
System.out.println(seg.segmentString("iateanant", dictonary));

}

}

- simplysou November 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can optimize by
1) stop looking when the remaining string is less than the longest legal string found to date.
2) only look for strings larger then the longest found to date.

- aclark November 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Breadth first search on suffix trie with maxheap as queue for BFS. Stop when you find the first legal word. Not sure what the running time would be, prob O(n^2) worst time.

- Eugen Hotaj November 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Without a preprocessing knowledge and limited control over legal words data structures, we can optimize the iterations

1. Short Circuit when first match for specific word length found,
2. Terminate program when first legal word found.
3. Start with longest word to shortest word.

public static string LongestWord(string word){
      string result= default(string);	 
	 if(string.IsNullOrEmpty(word)) { return word;}
	 
	     for(int wl = word.Length; wl > 1; wl--){
	     	for(int si = 0; si <= word.Length - wl  ; si++){
	     		var eword = word.Substring(si,wl);
	     		if(IsLegalWord(eword)){
	     			result = eword;
	     			break;
	     		}
	     	}
	     	if(result != default(string)) break;
	     }
	return result;
	}

- ankushbindlish November 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think we can do better than O(n^2), but we can do some optimizations as follows:

1: The first loop starts at zero, indicating the start of the string; but we do not need to go all the way to end; once we find a legal string with length K, then we know the start must be at least (K-1) indices before the end of the string

2: For the second loop, indicating the end of the string, start from the end and go backward, two notes here

2.1: once you find a legal word, you can break from the loop
2.2: you don't need to go all the way back to "i",

Here's the code

bool IsLegalWork( const char * s, int start, int end );
   void FindLongestLegal( const char * s )
   {
      int n = strlen(s);
      int i,j;

      int LegalStart = 0;
      int LegalLength = 0;

      for ( i = 0 ; i < n - LegalLength ; i++ )
      {
         for ( j = n-1 ; j > i + LegalLength ; j-- )
         {
            if ( LegalWord( s, i , j ) )
            {
               if ( LegalLength < j - i + 1 )
               {
                  LegalStart = i;
                  LegalLength = j - i + 1;
               }
               break;
            }
         }
      }
   }

- koosha.nejad November 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have used regular expression to match the words that are present in the dictionary with the string given. Here is my code and I believe that it takes less than O(n^2) time. Btw regex is very fast i believe.

import re
def getsegstring(S,word):
	dictionary = ['hello','ate','a','an','ant','i']
	legalwords = {}
	finalwords= []
	if len(S)>0:
		for i in dictionary:
			match =re.compile(i)
			if len(re.findall(match,S))>0:
				legalwords[re.findall(match,S)[0]] = len(re.findall(match,S)[0]) 
		maxi = max(legalwords.values())
		for k,v in legalwords.iteritems():
			if v == maxi:
				 finalwords.append(k)
		return boolval(finalwords,word)
       else:
               return False
		
def boolval(finalwords,word):
	if word in finalwords:
		return True
	else:
		return False
	
print getsegstring("iateanant","ant")
"""
Result : True

"""

- revanthpobala November 11, 2015 | 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