Open In App

Gojek Interview Experience for Internship (On-Campus) 2022

This problem was asked in the OA rounds of Gojek during my On-campus internship in 2022. The problem statement goes as follows:

This problem is not available anywhere over the internet, so when I solved it, I decided to contribute it here so that others can refer to it and get a glimpse of what kind of questions are asked in the OAs. 



At first, this question looked a bit tricky, but after giving it some time, I got the binary search approach to solve the problem. 

The C++ code is as follows:






#include <bits/stdc++.h>
using namespace std;
  
#define ll long long
  
bool isPossible(ll x, ll n, ll m, ll k){
    ll tot = x; // x number of processes are assigned to the kth processor.
      
    // Starting from (k-1)th index and (x-1) processes down to 0th index, 
    // assigning processes to processors in decreasing fashion.
      
    ll temp = x-1; 
    ll ind = k-1; 
    while(ind >= 0){
        if(temp < 1){
           tot += 1; // Since, we need to assign atleast one process to all the processors.
           ind--;
           continue;
        }
        tot += temp;
        ind--;
        temp--;
    }
      
    // Starting from (k+1)th index and (x-1) processes upto (m-1)th index,
    // assigning processes to processors in decreasing fashion.
      
    temp = x-1;
    ind = k+1; 
    while(ind < m){
        if(temp < 1){
           tot += 1; // Since, we need to assign atleast one process to all the processors.
           ind++;
           continue;
        }
        tot += temp;
        ind++;
        temp--;
    }
      
    // If total number of processes assigned in above scheduling
    // does not exceed the given number of processes then,
    // then x can be the possible maximum number of processes assigned to the kth processor.
      
    return tot <= n;
}
  
int main(){
    ll n; // Number of processes to be allocated.
    ll m; // Number of processors.
    ll k; // Processor at which processes assigned should to be maximum.
      
    cin >> n >> m >> k;
      
    ll low = 0, high = n;
    while(low <= high){
        ll mid = low+(high-low)/2;
          
        if(isPossible(mid, n, m, k)) low = mid+1;
        else high = mid-1;
    }
      
    cout << high << endl;
  
    return 0;
}


Article Tags :