Climbing the Leaderboard

Sort by

recency

|

93 Discussions

|

  • + 0 comments

    This would be a terrible interview question and is wasting people's time. The problem should be clarified by HackerRank.

    It expects output values that would arise if each player was ranked within continuous operation but handles input/output in batches. The pre-sorted player scores add to the confusion. The solver then needs to ignore available information (later player scores) and provide "wrong" or "stale" answers.

    We cannot practice eliciting and solving for requirements if they are not presented in the question and there is no interviewer.

  • + 0 comments

    C++ solution vector uniqueScores {}; vector result; int lastScore = ranked[0]; uniqueScores.push_back(lastScore); for(int i=1;i uniqueScores[mid]) { right = mid -1; } else { left = mid +1; } } int left_bound = (right>=0)? right:-1; int right_bound = (left< uniqueScores.size())?left:-1; if(left_bound == right_bound) { result.push_back(left_bound+1); } else if(left_bound == -1) { result.push_back(1); } else if(right_bound == -1) { result.push_back(uniqueScores.size()+1); } else { result.push_back(right_bound+1); } } return result;

  • + 0 comments
    from bisect import bisect
    
    def climbingLeaderboard(ranked, player):
        r = sorted(list(set(ranked)))
        return [len(r) + 1 - x for x in [bisect(r, s) for s in player]]
    
  • + 0 comments

    Java 8 - Uses the stream distince and a modified binary search search algorithem.

        public static List<Integer> climbingLeaderboard(List<Integer> ranked, List<Integer> player) {
            List<Integer> playerRanking = new ArrayList<Integer>();
            List<Integer> rankBins = ranked.stream().distinct().collect(Collectors.toList());
            
            for (Integer score: player) {
                playerRanking.add(rank(rankBins, score));
            }
            
            return playerRanking;
        }
        
        private static Integer rank(List<Integer> r, Integer s) {
            if (s>r.get(0)) {
                r.add(0,s);
                return 1;
            }
    
            if (s<r.get(r.size()-1)) {
                r.add(s);
                return r.size();
            }
    
            int l = 0; 
            int h = r.size()-1;
            int m = l+((h-l)/2);
            
            while(h-l>1) {
                if ( s < r.get(m) )
                    l=m;
                else
                    h=m;
                m = l+((h-l)/2);
            }
            
            int vl = r.get(l);
            int vh = r.get(h);
            if (s == vl) {
                return l+1 ;
            } else if (s == vh) {
                return h+1;
            } else {
                r.add(h, s) ;
                return h+1;
            }
        }
    
  • + 0 comments
    def climbingLeaderboard(ranked, player):
        ranked = sorted(set(ranked), reverse=True)  # Remove duplicates and sort in descending order
        ranks = []
        index = len(ranked) - 1  # Start from the lowest ranked
    
        for score in player:
            while index >= 0 and score >= ranked[index]:
                index -= 1  # Move up in the leaderboard
            ranks.append(index + 2)  # Append the rank
        return ranks