Interview Question

Country: United States

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

#include<iostream>
#include<vector>

using namespace std;

int main(){

int x,y;
cin>>x>>y;

vector<int> gv(x+1);
vector<int> rv(y+1);

int ans = 0;
int slibings = y + 1;
int mxKid = 0;

for (int g=1 ; g<=x ; ++g) {
cin>>gv[g];
ans+=(gv[g]* slibings);
mxKid+=gv[g];
}

bool isOk= true;
for (int k=1 ; k<=y ; ++k){
cin>>rv[k];

if(rv[k] > mxKid) {
isOk = false;
}

}

if (isOk) cout<<ans<<endl; else cout<<-1<<endl;

return 0; }

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

can you explain your logic

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

#include<iostream>
#include<vector>

using namespace std;

int main(){

int x,y;
cin>>x>>y;

vector<int> gv(x+1);
vector<int> rv(y+1);

int ans = 0;
int slibings = y + 1;
int mxKid = 0;

for (int g=1 ; g<=x ; ++g) {
cin>>gv[g];
ans+=(gv[g]* slibings);
mxKid+=gv[g];
}

bool isOk= true;
for (int k=1 ; k<=y ; ++k){
cin>>rv[k];

if(rv[k] > mxKid) {
isOk = false;
}

}

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

Looks like Integer linear programming problem to me. I guess the interviewer expects you to use DP here. However an alternative and more fun approach would be to use a specialized tool like z3 in Python where this kind of problems are easy to express:

from z3 import *

X = 3
Y = 2
G = [1,2,1]
K = [3,4]

a = [[Int('a_{}_{}'.format(i,j)) for j in range(Y)] for i in range(X)]
s = Optimize()

s.add(And([a[i][j] >= G[i] for i in range(X) for j in range(Y)]))
s.add(And([a[i][j] <= K[j] for i in range(X) for j in range(Y)]))
for j in range(Y):
s.add(Or([a[i][j] == K[j] for i in range(X)]))
for i in range(X):
s.add(Or([a[i][j] == G[i] for j in range(Y)]))

s.minimize(Sum([a[i][j] for i in range(X) for j in range(Y)]))
if s.check() == unsat:
print (-1)
else:
print (simplify(sum([s.model()[a[i][j]] for j in range(Y) for i in range(X)])))
print ([[s.model()[a[i][j]] for j in range(Y)] for i in range(X)])

Output:

12
[[3, 1], [2, 4], [1, 1]]

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.

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.