Grab Interview Question for Software Development Managers


Country: India
Interview Type: In-Person




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

There exist four cases:

All three numbers are equal to 0. The number of ways = f(0)C3 (where pCq is the number of ways of choosing q numbers from p numbers).
One number is equal to 0, the other two are equal to some x > 0: f(0) * f(x)C2.
Two numbers are equal to some x>0, the third is 2*x: f(x)C2 * f(2 * x).
The three numbers are x, y and x + y, 0 < x, y: f(x) * f(y) * f(x + y).

- Nits January 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

void printVector(const string& comment, const vector<int>& targetVector) {
cout << comment << endl;
for (const auto i : targetVector) {
cout << i << " ";
}
cout << endl;
}

bool isPresent(const vector<int>& target, int value) {
bool found = false;

auto iter = std::find(target.begin(), target.end(), value);
if (iter != target.end()) {
found = true;
}

return found;
}

int main () {
// vector<int> myVector = {1, 1, 1, 2, 2, 4, 6, 1, 2};
vector<int> myVector = {1, 1, 1, 2, 2};
vector<int> processedNumbers;

printVector("Input sample", myVector);

cout << "Found 1 : " << isPresent(myVector, 1) << endl;
cout << "Found 10 : " << isPresent(myVector, 10) << endl;

int tripletCount = 0;

vector<int> num1Processed;
for (const auto num1 : myVector) {
int num1Freq = 0; // We want to find num2 and num2 such that num1 = num2 + num3

if (isPresent(num1Processed, num1)) {
continue;
} else {
num1Processed.push_back(num1);
}

for (const auto num1Dup : myVector) {
if (num1Dup == num1) {
++num1Freq;
}
}


vector<int> num2Processed;
for (const auto num2 : myVector) {
int num3 = 0;
int num2Freq = 0;
int num3Freq = 0;
if (num2 > num1) {
continue;
}

if (isPresent(num2Processed, num2)) {
continue;
} else {
num2Processed.push_back(num2);
}

for (const auto num2Dup : myVector) {
if (num2Dup == num2) {
++num2Freq;
}
}

num3 = num1 - num2;
for (const auto num3Check : myVector) {
if (num3Check == num3) {
++num3Freq;
}
}

if (num2Freq && num3Freq) {
num2Processed.push_back(num3);
cout << "Frequency of num1 : " << num1 << ":" << num1Freq << endl;
cout << "Frequency of num2 : " << num2 << ":" << num2Freq << endl;
cout << "Frequency of num3 : " << num3 << ":" << num3Freq << endl;
int result1 = 0;
if (num2 == num3) {
result1 = (num2Freq) * (num2Freq-1);
cout << "Since num2 and num3 are same : " << num2 << " " << num3 << ", taking result1 value : " << result1 << endl;
result1 = result1;
} else {
result1 = num2Freq*num3Freq;
cout << "result1 contribution : " << result1 << " for tuple : " << num1 << ":" << num2 << ":" << num3 << endl;
}
tripletCount += result1;
}
}
}

cout << "Total triplet count : " << tripletCount << endl;

return 0;
}

- Kedar February 12, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

void printVector(const string& comment, const vector<int>& targetVector) {
        cout << comment << endl;
        for (const auto i : targetVector) {
                cout << i << " ";
        }
        cout << endl;
}

bool isPresent(const vector<int>& target, int value) {
        bool found = false;

        auto iter = std::find(target.begin(), target.end(), value);
        if (iter != target.end()) {
                found = true;
        }

        return found;
}

int main () {
//      vector<int> myVector = {1, 1, 1, 2, 2, 4, 6, 1, 2};
        vector<int> myVector = {1, 1, 1, 2, 2};
        vector<int> processedNumbers;

        printVector("Input sample", myVector);

        cout << "Found 1 : " << isPresent(myVector, 1) << endl;
        cout << "Found 10 : " << isPresent(myVector, 10) << endl;

        int tripletCount = 0;

        vector<int> num1Processed;
        for (const auto num1 : myVector) {
                int num1Freq = 0; // We want to find num2 and num2 such that num1 = num2 + num3

                if (isPresent(num1Processed, num1)) {
                        continue;
                } else {
                        num1Processed.push_back(num1);
                }

                for (const auto num1Dup : myVector) {
                        if (num1Dup == num1) {
                                ++num1Freq;
                        }
                }


                vector<int> num2Processed;
                for (const auto num2 : myVector) {
                        int num3 = 0;
                        int num2Freq = 0;
                        int num3Freq = 0;
                        if (num2 > num1) {
                                continue;
                        }

                        if (isPresent(num2Processed, num2)) {
                                continue;
                        } else {
                                num2Processed.push_back(num2);
                        }

                        for (const auto num2Dup : myVector) {
                                if (num2Dup == num2) {
                                        ++num2Freq;
                                }
                        }

                        num3 = num1 - num2;
                        for (const auto num3Check : myVector) {
                                if (num3Check == num3) {
                                        ++num3Freq;
                                }
                        }

                        if (num2Freq && num3Freq) {
                                num2Processed.push_back(num3);
                                cout << "Frequency of num1 : " << num1 << ":" << num1Freq << endl;
                                cout << "Frequency of num2 : " << num2 << ":" << num2Freq << endl;
                                cout << "Frequency of num3 : " << num3 << ":" << num3Freq << endl;
                                int result1 = 0;
                                if (num2 == num3) {
                                        result1 = (num2Freq) * (num2Freq-1);
                                        cout << "Since num2 and num3 are same : " << num2 << " " << num3 << ", taking result1 value : " << result1 << endl;
                                        result1 = result1;
                                } else {
                                        result1 = num2Freq*num3Freq;
                                        cout << "result1 contribution : " << result1 << " for tuple : " << num1 << ":" << num2 << ":" << num3 << endl;
                                }
                                tripletCount += result1;
                        }
                }
        }

        cout << "Total triplet count : " << tripletCount << endl;

        return 0;
}

- Kedar February 12, 2020 | Flag


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