Google Interview Question for Software Engineers


Country: United States




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

vector<string> CompareDocuments(const vector<string>& d1, const vector<string>& d2, int n)
{
	vector<string> ret;
	set<string> comm_set;

	unordered_map<string, int> str_idx;
	for (int i = 0; i < d1.size(); i++)
	{
		str_idx[d1[i]] = i;
	}

	int idx = 0;
	int prev_idx = 0;
	string com_str;
	while (idx + n -1 < d2.size())
	{
		com_str = "";
		prev_idx = 0;
		for (int i = 0; i <= n; i++)
		{
			if (i == n)
			{
				comm_set.emplace(com_str);
				break;
			}
			com_str += i==0?d2[idx + i]: " " + d2[idx + i];

			unordered_map<string, int>::iterator it = str_idx.find(d2[idx+i]);
			if (it == str_idx.end())
				break;
			else
			{
				if (i && it->second != prev_idx + 1)
					break;

				prev_idx = it->second;
			}
		}
		idx++;
	}

	for (auto i = comm_set.begin(); i != comm_set.end(); i++)
	{
		ret.emplace_back(*i);
	}

	return ret;
}

int main()
{
	vector<string> d1{ "Today", "is", "Sunday" };
	vector<string> d2{ "Today", "is", "Saturday" };

	//ret = {Today, is}
	vector<string> ret = CompareDocuments(d1, d2, 1);

	//ret = {Today is}
	ret = CompareDocuments(d1, d2, 2);

	//ret = empty, 0
	ret = CompareDocuments(d1, d2, 3);

	return 0;
}

- LANorth July 06, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

example output:
---
$ javac DocComparisonInNgrams.java
$ java DocComparisonInNgrams
if n=1, then the shared ngrams are [Today,is] (count == 2)
if n=2, then the shared ngrams are [Today is] (count == 1)
if n=3, then the shared ngrams are [] (count == 0)
---

where DocComparisonInNgrams.java is:

import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class DocComparisonInNgrams {
    public static void main(String args[]) {
	List<String> sampleDocumentsForTest = getSampleDocumentsForTest();

	for (int n=1; n <= 3; n++) {
	    Set<String> result = findSharedNGrams(sampleDocumentsForTest, n);

	    String sharedNGrams = "[";
	    if (result == null) {
		result = new HashSet<String>();
	    }

	    for(String sharedNGram : result) {
		if (!sharedNGrams.equals("[")) { sharedNGrams += ","; }
		sharedNGrams += sharedNGram;
	    }

	    sharedNGrams += "]";

	    System.out.println("if n=" + n + ", then the shared ngrams are " + sharedNGrams + " (count == " + result.size() + ")");
	}
    }

    protected static Set<String> findSharedNGrams(List<String> inputDocuments, int numberOfWordsInGram) {
	int numDocs = inputDocuments.size();
	Set<String> sharedNGrams = null;
	for (int docIdx = 0; docIdx < numDocs; docIdx++) {
	    Set<String> docNGrams = new HashSet<String>();
	    String doc = inputDocuments.get(docIdx);
	    // tokenize document into n-grams
	    //  - split string into words
	    String[] docInWords = doc.split(" ");
	    for (int wordIdx = numberOfWordsInGram; wordIdx < docInWords.length; wordIdx++) {
		String ngram = "";
		for (int idx = wordIdx - numberOfWordsInGram; idx < wordIdx; idx++) {
		    if (!ngram.equals("")) { ngram += " "; }
		    ngram += docInWords[idx];
		}
		if (ngram.equals("")) {
		    continue;
		}

		docNGrams.add(ngram);
	    }
	    // if first document, build set from that document
	    if (sharedNGrams == null) {
		sharedNGrams = docNGrams;
	    } else {
		// for each subsequent document, intersect this document's set with existing set (shared n grams only)
		Set<String> intersectedNGramSet = new HashSet<String>();
		for (String ngram : docNGrams) {
		    if (sharedNGrams.contains(ngram)) {
			intersectedNGramSet.add(ngram);
		    }
		}
		sharedNGrams = intersectedNGramSet;
	    }
	}

	return sharedNGrams;
    }

    protected static List<String> getSampleDocumentsForTest() {
	List<String> sampleDocuments = new ArrayList<String>();

	sampleDocuments.add("Today is Sunday.");
	sampleDocuments.add("Today is Saturday.");

	return sampleDocuments;
    }
}

- 9e67468d581d72fa March 12, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function findSharedNGrams(d1, d2){
    var t1=d1.split(' ');
    var t2=d2.split(' ');
    
    var l1=t1.length,l2=t2.length;
    var i=0,j=0;
    
    const sharedGrams = [];
    while(i<l1 || j<l2){
        if(t1[i] !== t2[j]){
            break;
        }else{
            sharedGrams.push(t1[i]);
        }
        i++;j++;
    }
    const map = new Map;
    let k=1;
    
    while(k<=3 && k<=sharedGrams.length){
        let ww = []
        for(let x=0;x<sharedGrams.length;x+=k){
            ww.push(sharedGrams.slice(x,x+k));
        }
        map.set(k,ww);
        k++;
    }
    for(let n=1;n<=3;n++){
        const value = map.has(n) ? map.get(n).join(" ") : "[]";
        const size =  map.has(n) ? map.get(n).length : 0;
        console.log(`if n = ${n} then number of duplicates is ${size} (${value})`)
    }
}

var d1 = "Today had an awesome lunch";
var d2 = "Today had an Very Tiring day";
findSharedNGrams(d1, d2);

- noob January 16, 2023 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What I understood is here in JS format:

function findSharedNGrams(d1, d2){
    var t1=d1.split(' ');
    var t2=d2.split(' ');
    
    var l1=t1.length,l2=t2.length;
    var i=0,j=0;
    
    const sharedGrams = [];
    while(i<l1 || j<l2){
        if(t1[i] !== t2[j]){
            break;
        }else{
            sharedGrams.push(t1[i]);
        }
        i++;j++;
    }
    const map = new Map;
    let k=1;
    
    while(k<=3 && k<=sharedGrams.length){
        let ww = []
        for(let x=0;x<sharedGrams.length;x+=k){
            ww.push(sharedGrams.slice(x,x+k));
        }
        map.set(k,ww);
        k++;
    }
    for(let n=1;n<=3;n++){
        const value = map.has(n) ? map.get(n).join(" ") : "[]";
        const size =  map.has(n) ? map.get(n).length : 0;
        console.log(`if n = ${n} then number of duplicates is ${size} (${value})`)
    }
}

var d1 = "Today had an awesome lunch";
var d2 = "Today had an Very Tiring day";
findSharedNGrams(d1, d2);

- Noob January 16, 2023 | 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