Sort by

recency

|

952 Discussions

|

  • + 0 comments
    public static List<String> bigSorting(List<String> unsorted) {
        Map<Integer, List<String>> map = unsorted.stream().collect(Collectors.groupingBy(String::length));
        List<String> result = new ArrayList<>();
        map.keySet().stream().sorted().forEach(key -> {
            List<String> r = map.get(key);
            r.sort((s1, s2) -> s1.compareTo(s2));
            result.addAll(r);
        });
        return result;
    }
    
  • + 0 comments

    Here is problem solution in python java c++ c and Javascript - https://programmingoneonone.com/hackerrank-big-sorting-problem-solution.html

  • + 0 comments

    Can someone help me understand why my program, written in JavaScript programming language, failed some test cases in solving bigSort challenge?

    First of all (or scenario 1), I solved the "bigSort challenge" with application of bubble sort algorithms:

    function bigSort(unsorted) { const size = unsorted.length;

    for (let i = 0; i <= size - 1; i++) {
        let isSwapped = false;
    
        // Compare two values at a time, place
        // the bigger value at the right side of the array
        // and place the lower value at the left side of
        // array.
        for (let j = 0; j <= size - 1; j++) {
            if (parseInt(unsorted[j + 1]) < parseInt(unsorted[j])) {
    
                // Temporarily hold the least value.
                const minValue = unsorted[j + 1];
    
                unsorted[j + 1] = unsorted[j];
                unsorted[j] = minValue;
    
                // Track and update whenever some value are swapped.
                isSwapped = true;
            }
    
            if (!isSwapped) break;
        }
    
        return unsorted;
    }
    

    To my surprise, this fails test: test case 2, test case 7 and some others.

    Secondly (or scenario 2), I modified the code with the language in built "sort()" method. I provided it with the callback compare function as follows:

    functiion bigSort(unsorted) {
        return unsorted.sort((a, b) => a.localeCompare(b, undefined, {numeric: true});
    }
    

    I am surprised when I see test case 2 and 7 marked failed.

    Folks, kindly help me understand why some test cases failed, specifically test case 2 and 7 in the two scenarios.

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/GAvltofMdYc

    vector<string> bigSorting(vector<string> u) {
        sort(u.begin(), u.end(), [](string l, string r){
            if(l.size() == r.size()) return l < r;
            return l.size() < r.size();
        });
        return u;
    }
    
  • + 0 comments

    def bigSorting(unsorted): # Write your code here

    unsorted_1 = []
    unsorted_2 = []
    for i in unsorted:
        if len(i) <= 31:
            unsorted_1.append(i)
        else:
            unsorted_2.append(i)
    
    n = len(unsorted_2)
    for i in range(n-1, 0, -1):
        for j in range(i):
            if len(unsorted_2[j]) > len(unsorted_2[i]) or (len(unsorted_2[j]) == len(unsorted_2[i]) and unsorted_2[j] > unsorted_2[i]):
                unsorted_2[i], unsorted_2[j] = unsorted_2[j], unsorted_2[i]
    
    return list(map(str, sorted(list(map(int, unsorted_1))))) + unsorted_2