Google Interview Question for Software Engineers


Country: United States




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

vector<int> GetRandomSublist(vector<int> A, int n)
{
	default_random_engine seed((random_device())());

	if (n < 0 || n >= A.size()) return {};

	for (int i = 0; i < n; i++)
	{
		uniform_int_distribution<int> rand_gen(i, A.size() - 1);
		int num = rand_gen(seed);
		swap(A[i], A[num]);
	}

	vector<int> ret;
	for (int i = 0; i < n; i++)
		ret.emplace_back(A[i]);
	sort(ret.begin(), ret.end());

	return ret;

}

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

public static int[] getSubArray(int [] array, int n){
Random random = new Random();
int[] result = new int[n];
int index = 0;
for(int i = 0;i<n;i++){
index = random.nextInt((array.length-n+i-index)+1)+index;
result[i]=array[index++];
}
return result;
}

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

public static int[] getSubArray(int [] array, int n){
    Random random = new Random();
    int[] result = new int[n];
    int index = 0;
    for(int i = 0;i<n;i++){
      index = random.nextInt((array.length-n+i-index)+1)+index;
      result[i]=array[index++];
    }
    return result;
  }

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

def get_increasing_list(l, n):
q = [0, 1, 2]
print l[:n]
k = len(l)
visited = []
while len(q) != 0:
num = q.pop()
for i in range(num+1, k):
q.append(i)
if len(q) >= n and q[:n] not in visited:
res = map(lambda x: l[x], q)
print res
visited.append(q[:n])

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

def get_increasing_list(l, n):
    q = [0, 1, 2]
    print l[:n]
    k = len(l)
    visited = []
    while len(q) != 0:
        num = q.pop()
        for i in range(num+1, k):
            q.append(i)
            if len(q) >= n and q[:n] not in visited:
                res = map(lambda x: l[x], q)
                print res
                visited.append(q[:n])

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

def get_perm(nums, k):
    if k == 1 :
        for n in nums:
            yield [n]
    else:
        for i,n in enumerate(nums):
            for x in get_perm(nums[i+1:],k-1):
                yield [n] + x

def main(nums,k):
    if len(nums) < k:
        return []
    return list(get_perm(nums,k))

main([1,2,3,4,5], 3)

- python August 10, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//make the ans, temp vector in main. v is the given vector
void func(vector<int> &temp, vector<int> v, int i, int m, vector<vector<int> > &ans) {
if(temp.size() == m) {
ans.emplace_back(temp);
return;
}
for(int j = i; j < v.size(); j++) {
temp.push_back(v[j]);
func(temp, v, j + 1, m, ans);
temp.pop_back();
}
}

- Aheli Ghosh August 11, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void func(vector<int> &temp, vector<int> v, int i, int m, vector<vector<int> > &ans){
if(temp.size() == m){
ans.emplace_back(temp);
return;
}
for(int j = i; j < 5; j++){
temp.push_back(v[j]);
func(temp, v, j + 1, m, ans);
temp.pop_back();
}
}

- Aheli Ghosh August 11, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import random
def generate_n_list(lst, n):
    if n > len(lst):
        return []
    if n == len(lst):
        return lst
    res = []

    for i in range(len(lst) - 1,-1,-1):
        j = random.randrange(0,i + 1,1)
        temp = lst[j]
        lst[j] = lst[i]
        lst[i] = temp
        res.append(temp)

        if len(res) == n:
            break

    return sorted(res)

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

it seems to not respect the requirements: this solution allows to have descending order

- lulu September 09, 2019 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void sublist(int[] L, int n) {
for (int i=0; i <5; i++){
for (int j=i+1; j < 5; j++ ){
for (int k=j+1; k < 5; k++){
if (L[i] <L[j] && L[j]<L[k]){
System.out.println(L[i] +"," + L[j] + " ," + L[k] + "");
}
}
}
}
}

- Anonymous July 05, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void sublist(int[] L, int n) {
for (int i=0; i <5; i++){
for (int j=i+1; j < 5; j++ ){
for (int k=j+1; k < 5; k++){
if (L[i] <L[j] && L[j]<L[k]){
System.out.println(L[i] +"," + L[j] + " ," + L[k] + "");
}
}
}
}
}

- code gru July 05, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

{
my name is varma
}

- Anonymous August 10, 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