Five Star Seller/Maximum Average Pass Ratio Solution Amazon OA 2021

Five Star Seller/Maximum Average Pass Ratio Solution

There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array classes, where classes[i] = [passi, totali]. You know beforehand that in the ith class, there are totali total students, but only passi number of students will pass the exam.

You are also given an integer extraStudents. There are another extraStudents brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents students to a class in a way that maximizes the average pass ratio across all the classes.

The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes.

Return the maximum possible average pass ratio after assigning the extraStudents students. Answers within 10-5 of the actual answer will be accepted.

See Also : Amazon Online Assessment Questions 2021 Preparation

Example 1:

Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2
Output: 0.78333
Explanation: You can assign the two extra students to the first class. The average pass ratio will be equal to (3/4 + 3/5 + 2/2) / 3 = 0.78333.

Example 2:

Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4
Output: 0.53485


  • 1 <= classes.length <= 105
  • classes[i].length == 2
  • 1 <= passi <= totali <= 105
  • 1 <= extraStudents <= 105


Program C++

class Solution {
    typedef pair<double, pair<int, int>> ii;
    double ratio(int x, int y) {
        return (double) x / y;
    double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
        double ans = 0;
        int k = extraStudents;
        priority_queue<ii> pq;
        for(auto c : classes)
            pq.push({ ratio(c[0]+1, c[1]+1) - ratio(c[0], c[1]) , {c[0], c[1]}});
        while(!pq.empty() && k) {
            auto curr =;
            int x = curr.second.first;
            int y = curr.second.second;
            pq.push({ratio(x+2, y+2) - ratio(x+1, y+1) , {x+1, y+1}});
        while(!pq.empty()) ans += ratio(,, pq.pop();
        return ans / classes.size();

Program Java

class Classes{
    int pass;
    int total;
    double incr;
    Classes(int pass,int total,double incr){

class Heap{
    private int heapSize;
    Classes[] data;
    Heap(int heapSize,int[][] classes){
        this.heapSize = heapSize;
        data = new Classes[heapSize];
        for(int i=0;i<heapSize;i++){
            double newIncr = (double)((classes[i][0]+1)/(double)(classes[i][1]+1)) -
            data[i]=new Classes(classes[i][0],classes[i][1],newIncr);
    void buildHeap(){
        for(int i=Parent(heapSize-1);i>=0;i--)heapify(i);
    void heapify(int index){
            int greatestChildIndex = findGreatestChildIndex(index);
    int Parent(int index){
        return (index-1)/2;
    int findGreatestChildIndex(int parent){
            if([2*parent+1].incr, data[2*parent+2].incr)<0)return 2*parent+2;
            //if(data[2*parent+1].incr < data[2*parent+2].incr)return 2*parent+2;
            return 2*parent+1;
        }else return 2*parent+1;
    boolean rightChildExist(int parent){
        if(parent*2+2<heapSize)return true;
        return false;
    boolean isLeaf(int index){
        if(2*index+1<heapSize)return false;
        return true;
    void swap(int a,int b){
        Classes temp = data[a];
    Classes delMin(){
        Classes min = data[0];
        return min;
    void insertPoint(Classes p){[heapSize]=p;
        int index=Parent(heapSize-1);
        int childIndex = heapSize-1;
            if(data[index].incr < data[childIndex].incr){
                int temp = index;
                childIndex = temp;
            }else break;

class Solution {
    public double maxAverageRatio(int[][] classes, int extraStudents) {
        double currentAvg = 0;
        for(int i=0;i<classes.length;i++){
            double val=classes[i][0]/(double)classes[i][1];
        Heap heap = new Heap(classes.length,classes);
        for(int i=0;i<extraStudents;i++){
            Classes temp = heap.delMin();
            double newIncr = ((temp.pass+1)/(double)( -
        return currentAvg/classes.length;

Program Python

In iterative allocations of extra students, we note the following:

  1. The gain in class average (mu) from an extra students for any given class can be computed in iterative fashion with respect to iterative class sizes of k-1 and k.
    mu_{k} = mu_{k-1} + ( 1 - mu_{k-1} ) / k
  2. The maximum iterative gain in overall average of class averages corresponds to the greatest class average gain derived in step 1 scaled by n (the total number of classes).
  3. We can use a heap to quickly derive the maxmimum iterative gain in step 1 in log n time per iteration.

Complexity Analysis:
O(e log n); e == extraStudents; n == len(classes);

from heapq import *

class Solution:
    def maxAverageRatio(self, classes: List[List[int]], extraStudents: int) -> float:
        computeGain = lambda p, t: (1 - (p / t)) / (t + 1)
        R = [(-computeGain(p, t), p, t) for p, t in classes]

        for x in range(extraStudents):
            _, p, t = heappop(R)
            p += 1
            t += 1
            heappush(R, (-computeGain(p, t), p, t))

        return sum(map(lambda x: x[1] / x[2], R)) / len(classes)


Weekly Contest 247

Biweekly Contest 55

June Long Challenge 2021 Solutions

Leave a Comment

Please Click on 1 or 2 Ads to help us run this site.