Google Interview Question
Software EngineersCountry: United States
Interview Type: Phone Interview
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;
}
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;
}
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;
}
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;
}
}
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;
}
};
}
}
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;
}
};
}
}
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;
}
};
}
}
Loop through the list starting from index i=1 and keep comparing the value at ith position
with (i-1)th, if it is same then increase the counter by 1 (else case in following code).
If it is not same (elif case in following code) then add the string and counter value to your result and
reset the counter value. Handle edge case when i=length of the input list (if case in following code).
# In Python3
def wordCount(str_list):
count_list = []
count = 0
for i in range(1,len(str_list)+1):
if i == len(str_list):
if str_list[i-1] == str_list[i-2]:
count_list.append((str_list[i-1],count+1))
else:
count_list.append((str_list[i-2],count+1))
count_list.append((str_list[i-1],1))
elif str_list[i] != str_list[i-1]:
count_list.append((str_list[i-1],count+1))
count=0
else:
count += 1
return count_list
str_list= ['foo','foo','foo','bar','foo','bar','bar']
print (wordCount(str_list))
Console Output: [('foo', 3), ('bar', 1), ('foo', 1), ('bar', 2)]
- oldengineer May 04, 2019