Interview Question


Country: United States




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

This can be solved by inverting the Map which is genre map.
Once we invert the genre map, we know genre of each song, and now we can
traverse the song list of people while counting the genre of each song, and bingo!

This translates into :

people = { 
  "David": ["song1", "song2", "song3", "song4", "song8"],
  "Emma": ["song5", "song6", "song7"] }

genres = {
  "Rock": ["song1", "song3"],
  "Dubstep": ["song7"],
  "Techno": ["song2", "song4"],
  "Pop": ["song5", "song6"],
  "Jazz": ["song8", "song9"]
}
song_to_genre = fold( genres.entries , dict() ) as {
  genre = $.key 
  song_list = $.value  
  for ( song : song_list ){
    $.p[song] = genre 
  } 
  $.p // return 
}
// now the trivial counting matter 
for ( person : people.keys ){
  persons_songs = people[person] 
  counter = dict()
  for ( song : persons_songs ){
    if ( song !@ counter ){
      counter[song_to_genre[song]] = 0
    }
    counter[song_to_genre[song]] += 1
  }
  // find the genre with maximal count now?
  #(min,Max) = minmax( counter.entries ) where { sign( $.left.value - $.right.value ) }
  printf("Person : %s , Genre: %s %n", person, Max.key )
}

- NoOne November 25, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Map<String, List<String>> personMusic = new HashMap<>(  );
       Map<GENTYPE, List<String>> generic = new LinkedHashMap<>(  );

       personMusic.put( "Chaitu", new ArrayList<>(  ) );
       personMusic.put( "Abhi", new ArrayList<>(  ) );

        personMusic.get("Chaitu").add( "S1" );
        personMusic.get("Chaitu").add( "S2" );
        personMusic.get("Chaitu").add( "S3" );
        personMusic.get("Chaitu").add( "S4" );
        personMusic.get("Chaitu").add( "S8" );

        personMusic.get("Abhi").add( "S5" );
        personMusic.get("Abhi").add( "S6" );
        personMusic.get("Abhi").add( "S7" );


        generic.put( GENTYPE.Rock,new ArrayList<>(  ) );
        generic.put( GENTYPE.Dubstep,new ArrayList<>(  ) );
        generic.put( GENTYPE.Techno,new ArrayList<>(  ) );
        generic.put( GENTYPE.Pop,new ArrayList<>(  ) );
        generic.put( GENTYPE.Jazz,new ArrayList<>(  ) );

        generic.get( GENTYPE.Rock ).add( "S1" );
        generic.get( GENTYPE.Rock ).add( "S3" );
        generic.get( GENTYPE.Dubstep ).add( "S7" );
        generic.get( GENTYPE.Techno ).add( "S2" );
        generic.get( GENTYPE.Techno ).add( "S4" );
        generic.get( GENTYPE.Pop ).add( "S5" );
        generic.get( GENTYPE.Pop ).add( "S6" );
        generic.get( GENTYPE.Jazz).add( "S8" );
        generic.get( GENTYPE.Jazz ).add( "S9" );

        System.out.print( generic );

        Map<String, String > favGen = new HashMap<>(  );

        personMusic.entrySet().stream().forEach( person->{
            List<String> personSongs = person.getValue();
            int songsCounter=0;
            for(Map.Entry <GENTYPE, List<String>> entry : generic.entrySet()){
                List<String> tempList = new ArrayList<>( personSongs );
                tempList.retainAll( entry.getValue());
                if(tempList.size() > songsCounter){
                    songsCounter = tempList.size();
                    favGen.put(person.getKey(), entry.getKey().getCode());
                }
            }
        } );

        System.out.println( favGen );

- Chaitanya Srikakolapu November 26, 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