Google Interview Question for Software Engineers


Country: United States




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

Simply, since each query has a count, we build array of ranges and each range represent a query, then we use rand()%countSum , where countSum is the sum of all counts for all queries, after that we do binary search to see this fits in which range and we return the query that represent this range !

here is the code, O(N)

class Range : public pair<int,int>{
public:
	int queryIndex;

	Range(int f, int s, int qindex=0) : pair<int, int>(f, s), queryIndex(qindex){}



};


int binarySearch(vector< Range >& rangesList,int value, int L, int R){
	
	if (L <= R)
	{

		int m = (L + R) / 2;

		if (rangesList[m].first <= value && value <= rangesList[m].second)
			return m;
		else
		if (value > rangesList[m].second){
			return binarySearch(rangesList, value, m + 1, R);
		}
		else{
			return binarySearch(rangesList, value, L, m - 1);
		}
	}

	return -1;
}

string getRandomQuery(const vector< pair<string, int> >& queries){

	vector< Range > rangesList;

	int rangeCount = 0;

	int countSum = 0;

	for (int i = 0; i < queries.size(); i++){
		Range range(rangeCount, rangeCount + queries[i].second - 1, i);
		rangesList.push_back(range);

		countSum += queries[i].second;

		rangeCount += queries[i].second;
	}

	int randomValue = rand() % countSum;

	int index = binarySearch(rangesList, randomValue, 0, rangesList.size() - 1);

	return queries[index].first;
}

- LaithBasilDotNet May 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Overall, it's good. But you don't have to keep ranges - it's enough to have just a regular int array Bounds, so that Bounds[0] = Counts[0] and Bounds[i + 1] = Bounds[i] + Counts[i + 1]. Index in that array found with BinSearch is an index of the result query.

- Alex M. June 02, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

input - 
query - count
q1 	- 	3
q2 	-	5
q3	-	2
sum = 10; ( sum of all counts)

for(int i=0;i<k;i++) {
	int r = rand()%sum;
	if( !( r/count1) )
		select = q1; count1--;
	else if ( !(r/count1+count2) )
		select= q2; count2--;
	else
		select= q3; count3--;
	sum--;
	print select;
}

- ~amit May 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If I understand the answer properly, we will be getting a frequency of each query and we will use that as weighting for which query to return. In Python, I have made the assumption that the data is formatted as a list of tuples where the tuple contains the query and the count. E.g. [('dog', 5), ('cat', 7), ('cow', 2)]

from random import randint


def rand_query(queries):
    data = []
    x = 0
    for query in queries:
        data.append((query, x, x+query[1]))
        x += (query[1] + 1)

    max = sum(x[1] for x in queries)
    rand = randint(0, max)

    for entry in data:
        if rand >= entry[1] and rand <= entry[2]:
            return entry[0][0]


if '__main__' == __name__:
    print(rand_query([('dog', 5), ('cat', 7), ('cow', 2)]))

- drincruz June 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function QueryGenerator(queries) {
    
    //calculate total of all scores
    var sum = queries
        .map(function(x){return x[1];})
        .reduce(function(a,x) { return a+x;});

    var score = {};
    var scoreSum = 0;

    return function getQuery() {

        while (true) {

            if (scoreSum === sum) {
                //invalidate score
                score = {};
                scoreSum = 0;
            }

            var rand = Math.round(Math.random() * (queries.length - 1));

            var query = queries[rand][0];
            var freq = queries[rand][1];

            if (score[query] === undefined) {
                score[query] = 1;

                scoreSum++;
                return query;
            }
            else if (score[query] < freq) {
                score[query]++;

                scoreSum++;
                return query;
            }
        }
    }
}

- alexey June 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here's test

var queries = [
    ['One', 1],
    ['Two', 2],
    ['Three', 3]
];

var generator = QueryGenerator(queries);
//verify
var stats = {};
for(var i =0; i < 1000; i++) {
    var q = generator();
    if(stats[q] === undefined) stats[q] = 0;
    stats[q]++;
}
console.log(stats);

- alexey June 24, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

similar to shuffling a deck of cards. However, in this case you have counts associated with each 'card/query'.

- nik August 09, 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