Google Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

package sandbox;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class WordCount implements Iterable<WordCount> {

    private  String word;
    private int count;

    public void setWord(String word) {
        this.word = word;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getWord() {
        return word;
    }

    public int getCount() {
        return count;
    }

    private WordCountIterator wordCountIterator;

    @Override
    public Iterator<WordCount> iterator() {
        return wordCountIterator;
    }

    public  WordCount(Iterator<String> value) {

        wordCountIterator =  new WordCountIterator(value);

    }

    public  WordCount() {
    }


    public class WordCountIterator implements  Iterator<WordCount> {
        
        private LinkedList<WordCount> wordsCount;

        private  int index;

        public WordCountIterator(Iterator<String> wordCount) {
            String prevWord = new String();
            int prevCount = 0;
            while( wordCount.hasNext() ) {
                WordCount wordcount = new WordCount();
                if(wordsCount == null){
                    wordsCount = new LinkedList<>();
                    prevWord = wordCount.next();
                    prevCount = 1;
                }
                else {
                    String currword = wordCount.next();
                    if(prevWord.equals(currword)) {
                        prevCount += 1;
                    } else {
                        wordcount.word = prevWord;
                        wordcount.count = prevCount;
                        wordsCount.add(wordcount);
                        prevWord = currword;
                        prevCount = 1;
                    }
                }
            }
            if(prevCount != 0 ){
                WordCount wordcount = new WordCount();
                wordcount.word = prevWord;
                wordcount.count = prevCount;
                wordsCount.add(wordcount);
            }
            index = 0;
        }

        @Override
        public boolean hasNext() {
            return index < wordsCount.size() ;
        }

        @Override
        public WordCount next() {
            return  wordsCount.get(index++);

        }
    }

    public static  void main(String[] args){
        String[] input = {"foo","foo","foo","bar","foo","bar","bar"};
        List<String> str = new ArrayList<>();
        for(int i = 0; i < input.length; i++){
            str.add(input[i]);
        }
        WordCount wordCount = new WordCount(str.iterator());
        Iterator<WordCount> wordIterator = wordCount.iterator();
        while(wordIterator.hasNext()){
            WordCount word = wordIterator.next();
            System.out.println(word.word + " " + word.count);
        }
    }
}

- oldengineer May 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

groupList :: [String] -> [(String, Int)]
groupList xs = ret where 
  ret = List.map (\x -> (List.head x, List.length x)) . List.group $ xs

- Keep_learning May 11, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ solution

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{

vector<string> words = {"foo","foo","foo","bar","foo","bar","bar"};

int count=0;
string ref;
struct data {
string word;
int count;
};
vector<data> result;
for (int i=0; i<words.size(); i++) {
if (i==0) ref=words[1];
if (ref == words[i] ) {

count++;
} else {

struct data d;
d.word=ref;
d.count=count;
result.push_back(d);
ref=words[i];
count=1;

}
if (i==words.size()-1 ) {
struct data d;
d.word=ref;
d.count=count;
result.push_back(d);
}

}


for (int i=0; i<result.size(); i++) {
cout << result[i].word<<":"<<result[i].count<<endl;
}

return 0;
}

- Jeff C May 05, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ solution

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    
    vector<string> words = {"foo","foo","foo","bar","foo","bar","bar"};
    
    int count=0;
    string ref;
    struct data {
        string word;
        int count;
    };
    vector<data> result;
    for (int i=0; i<words.size(); i++) {
        if (i==0) ref=words[1];
        if (ref == words[i] ) {
            
            count++;
        } else {
            
            struct data d;
            d.word=ref;
            d.count=count;
            result.push_back(d);
            ref=words[i];
            count=1;
            
        }
        if (i==words.size()-1 ) {
            struct data d;
            d.word=ref;
            d.count=count;
            result.push_back(d);  
        }
        
    }

    
    for (int i=0; i<result.size(); i++) {
        cout << result[i].word<<":"<<result[i].count<<endl;
    }

    return 0;
}

- Jeff C May 05, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ solution:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    
    vector<string> words = {"foo","foo","foo","bar","foo","bar","bar"};
    
    int count=0;
    string ref;
    struct data {
        string word;
        int count;
    };
    vector<data> result;
    for (int i=0; i<words.size(); i++) {
        if (i==0) ref=words[1];
        if (ref == words[i] ) {
            
            count++;
        } else {
            
            struct data d;
            d.word=ref;
            d.count=count;
            result.push_back(d);
            ref=words[i];
            count=1;
            
        }
        if (i==words.size()-1 ) {
            struct data d;
            d.word=ref;
            d.count=count;
            result.push_back(d);  
        }
        
    }

    
    for (int i=0; i<result.size(); i++) {
        cout << result[i].word<<":"<<result[i].count<<endl;
    }

    return 0;
}

- Jeff C May 05, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.google;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class WordCount implements Iterable<WordCount> {

	String word;
	int count;

	WordsCountIterator iterator;

	public WordCount(List<String> s) {

		Iterator<String> it = s.iterator();
		iterator = new WordsCountIterator(it);

	}

	public WordCount() {

	}

	class WordsCountIterator implements Iterator<WordCount> {

		List<WordCount> wordCounts;
		int index = 0;

		public WordsCountIterator(Iterator<String> iter) {

			String prevWord = null, currWord = null;
			int prevCount = 1;
			while (iter.hasNext()) {

				if (null == wordCounts) {
					wordCounts = new LinkedList<>();
					prevWord = iter.next();

				} else {
					currWord = iter.next();
					if (prevWord.equals(currWord)) {
						prevCount++;

					} else {
						WordCount wc = new WordCount();
						wc.count = prevCount;
						wc.word = prevWord;
						wordCounts.add(wc);
						prevWord = currWord;
						prevCount = 1;
					}
				}

			}

		}

		@Override
		public boolean hasNext() {

			return index < wordCounts.size();
		}

		@Override
		public WordCount next() {

			return wordCounts.get(index++);
		}

	}

	public static void main(String[] args) {
		String[] input = { "foo", "foo", "foo", "bar", "foo", "bar", "bar" };
		List<String> str = new ArrayList<>();
		for (int i = 0; i < input.length; i++) {
			str.add(input[i]);
		}
		WordCount wordCount = new WordCount(str);
		Iterator<WordCount> wordIterator = wordCount.iterator();
		while (wordIterator.hasNext()) {
			WordCount word = wordIterator.next();
			System.out.println(word.word + " " + word.count);
		}
	}

	@Override
	public Iterator<WordCount> iterator() {

		return iterator;
	}
}

- shukad333 May 07, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

class WordAndCount {
	public Iterator wnc(Iterator<String> itor){
		return new Iterator() {
			Map<String,Integer> map = new HashMap<>();
			Iterator<String> argItor = itor;
			
			public boolean hasNext() {
				return argItor.hasNext() || !map.isEmpty();
			}
			public Map.Entry<String,Integer> next() {
				while(hasNext()) {
					if(argItor.hasNext()) {
						String inp = argItor.next();
						if(map.isEmpty()) {
							map.put(inp, 1);
						} else {
							if(map.containsKey(inp)) {
								int v = map.get(inp);
								map.put(inp, ++v);
							} else {
								String key = map.keySet().toArray(new String[0])[0];
								Map.Entry<String, Integer> wordAndCount = 
									new AbstractMap.SimpleEntry<String, Integer>(key, map.get(key));
								map.clear();
								map.put(inp, 1);
								return wordAndCount;
							}
						}
					} else {
						String key = map.keySet().toArray(new String[0])[0];
						Map.Entry<String, Integer> wordAndCount = 
							new AbstractMap.SimpleEntry<String, Integer>(key, map.get(key));
						map.clear();
						return wordAndCount;
					}
				}
				return null;
			}
		};
	}
}

- sujitroutray@hotmail.com May 11, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class WordAndCount {
	public Iterator wnc(Iterator<String> itor){
		return new Iterator() {
			Map<String,Integer> map = new HashMap<>();
			Iterator<String> argItor = itor;
			
			public boolean hasNext() {
				return argItor.hasNext() || !map.isEmpty();
			}
			public Map.Entry<String,Integer> next() {
				while(hasNext()) {
					if(argItor.hasNext()) {
						String inp = argItor.next();
						if(map.isEmpty()) {
							map.put(inp, 1);
						} else {
							if(map.containsKey(inp)) {
								int v = map.get(inp);
								map.put(inp, ++v);
							} else {
								String key = map.keySet().toArray(new String[0])[0];
								Map.Entry<String, Integer> wordAndCount = 
									new AbstractMap.SimpleEntry<String, Integer>(key, map.get(key));
								map.clear();
								map.put(inp, 1);
								return wordAndCount;
							}
						}
					} else {
						String key = map.keySet().toArray(new String[0])[0];
						Map.Entry<String, Integer> wordAndCount = 
							new AbstractMap.SimpleEntry<String, Integer>(key, map.get(key));
						map.clear();
						return wordAndCount;
					}
				}
				return null;
			}
		};
	}
}

- Anonymous May 11, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class WordAndCount {
	public Iterator wnc(Iterator<String> itor){
		return new Iterator() {
			Map<String,Integer> map = new HashMap<>();
			Iterator<String> argItor = itor;
			
			public boolean hasNext() {
				return argItor.hasNext() || !map.isEmpty();
			}
			public Map.Entry<String,Integer> next() {
				while(hasNext()) {
					if(argItor.hasNext()) {
						String inp = argItor.next();
						if(map.isEmpty()) {
							map.put(inp, 1);
						} else {
							if(map.containsKey(inp)) {
								int v = map.get(inp);
								map.put(inp, ++v);
							} else {
								String key = map.keySet().toArray(new String[0])[0];
								Map.Entry<String, Integer> wordAndCount = 
									new AbstractMap.SimpleEntry<String, Integer>(key, map.get(key));
								map.clear();
								map.put(inp, 1);
								return wordAndCount;
							}
						}
					} else {
						String key = map.keySet().toArray(new String[0])[0];
						Map.Entry<String, Integer> wordAndCount = 
							new AbstractMap.SimpleEntry<String, Integer>(key, map.get(key));
						map.clear();
						return wordAndCount;
					}
				}
				return null;
			}
		};
	}
}

- AskSuz May 11, 2019 | 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