Open In App

Gojek Interview Experience for Internship (On-Campus) 2022

Improve
Improve
Like Article
Like
Save
Share
Report

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

  • There are n processes that need to be assigned to m processors such that the kth processor should have the maximum number of processes (the kth processor is the most efficient one). Also, each processor must be assigned at least one process and the number of processes assigned to the adjacent processors must not exceed 1. You need to find the maximum number of processes that can be assigned to the kth processor following all the given constraints.

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:

C++




#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;
}




Last Updated : 19 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads