Apple | Phone | Basic sorting

Anonymous User
2178
Feb 05, 2022
Feb 05, 2022

Basically had a phone interview today and was pretty disappointed with the interviewer. I was told to compare two strings based on the second character. If we have list like this [["Hello", 2, 2.2342], ["World", 1, 2.7589], ["Jello", 3, 2.8373]]. It basically has to return [["World", 1, 2.7589], ["Hello", 2, 2.2342], ["Jello", 3, 2.8373]]. I suggested using a comparator since my preferred language is Java but I was unsure of the syntax and he wasn't positive on me checking me how to use it. I basically implemented comparator using Collections.sort() then checked the .split(",") and then compared str1[1] to str2[1]. He seemed like he wanted a one liner instead. I suggested something like this.

		list.add("hello,1, 2.672");
        list.add("Gello,3, 3.14");
        list.add("adsfaello,2, 4.123");
        // Sort the array based on the second input
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                String[] split1 = o1.split(",");
                String[] split2 = o2.split(",");
                return Integer.parseInt(split2[1]) - Integer.parseInt(split1[2]);
            }
        });
        System.out.println(list);

I then suggested a HashMap and PQ and he liked the of a HashMap but was confused on the PQ. I am not sure what he was looking for before he cut the call he told me its an easy one liner. I feel really stupid. Did I just over think the problem too much or is there an actual one liner?

Comments (11)