Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

String[] strArr = new String[]{"geeks", "for", "geeks", "a", "portal", "to", "learn", "can", "be", "computer", "science", "zoom", "yup", "fire", "in", "be", "data", "geeks"};
        List<String> strs = Arrays.asList(strArr);
        Map<String, Long> newMap = strs.stream().collect( Collectors.groupingBy( s -> s, Collectors.counting() ) );

        strs = newMap.entrySet().stream().filter( stringLongEntry -> stringLongEntry.getValue() > 1 )
                .sorted( (o1, o2) -> o2.getValue().intValue() - o1.getValue().intValue())
                .map( entry-> entry.getKey() ).collect(Collectors.toList());

- Chaitanya Srikakolapu November 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Clever usage of Java 8 Stream. But, I think the filter usage for a counter larger than 1 isn't right for what the question asks for. It asks for first N the highest frequency words. I think the code shall be the following:

strs = newMap.entrySet().stream().sorted( (o1, o2) -> o2.getValue().intValue() - o1.getValue().intValue()).map( entry-> entry.getKey() ).collect(Collectors.toList()).subList(0, k);

- fz November 26, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

const obj = {};
["geeks", "for", "geeks", "a", "portal", "to", "learn", "can", "be", "computer", "science", "zoom", "yup", "fire", "in", "be", "data", "geeks"].forEach(item => {
    if(obj[item] === undefined) {
        obj[item] = 1;
    } else {
        obj[item]++;
    }
});
Object.entries(obj).sort((a, b) => a[1] - b[1]).slice(-2).reduce((arr, item) => {
    arr.push(item[0]);
    return arr;
}, []);

JavaScript

- nikomarow November 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What language is it?

- fz November 26, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

N = 2 
l = [ "geeks", "for", "geeks", "a", "portal", "to", 
      "learn", "can", "be", "computer", "science", "zoom", 
      "yup", "fire", "in", "be", "data", "geeks"]
counter = list ( mset(l) ) as { [ $.key, $.value ] }
// option 1 : not good using sorting -- bad idea for large data set 
result = sortd( counter ) where { sign($.l.1 - $.r.1 ) }
println( str( [0:N] , "\n" ) as { str("%s -> %d", result[$.o][0],result[$.o][1] ) } )
// option 2 : better use max heap 
h = heap( N ) where { sign($.r.1 - $.l.1 ) }
h +=  result
println( str( [N-1:-1] , "\n" ) as { str("%s -> %d", h[$.o][0],h[$.o][1] ) } )

- NoOne November 29, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More