Climbing the Leaderboard

Sort by

recency

|

97 Discussions

|

  • + 0 comments

    Problem with Javascript rendered this unsolvable

  • + 0 comments

    Simple work around to achieve O(n) time and space complexity taking advantage of the fact that the array is already sorted. Beats the other solutions since we do not have to sort the array again or run binary search n times. Same space complexity as we have to make a new array for the solution anyway.

    def climbingLeaderboard(ranked, player):
        cleanRanked = []
        for score in ranked:
            if len(cleanRanked) > 0 and cleanRanked[-1] == score:
                continue
            cleanRanked.append(score)
            
        res = []
        r = len(cleanRanked) - 1
        for score in player:
            while r >= 0 and score >= cleanRanked[r]:
                r -= 1
            res.append(r+2)
        
        return res
    
  • + 0 comments

    Binary search would do and turns out it was pretty straight forward. Here is my snippet in Javascript.

    function binarySearch(arr, val) {
        let start = 0;
        let end = arr.length - 1;
    
        while (start <= end) {
            const mid = Math.floor((start + end) / 2);
    
            if (arr[mid] === val) {
                return mid + 1; // Found the exact value
            } else if (val > arr[mid]) {
                end = mid - 1; // Search in the left half
            } else {
                start = mid + 1; // Search in the right half
            }
        }
    
        // Determine the rank based on the insertion position
        return start + 1;
    }
    
    function climbingLeaderboard(ranked, player) {
        // Write your code here
        const uniqRanked = [...new Set(ranked)];
        return player.map((score) => {
            return binarySearch(uniqRanked, score);
        });
    }
    
  • + 0 comments

    be careful with the time limit exceed, so annoying

    the program should be sort desc (start from last)

    ranked = sorted(set(ranked), reverse=True)
        
        res = []
        i = len(ranked) - 1  # Start from the last element in the ranked list
    
        for score in player:
            while i >= 0 and score >= ranked[i]:
                i -= 1
            res.append(i + 2) 
        
        return res
    
  • + 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.