LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · Exercise 4 of 4

HashMap and TreeMap HashMap and TreeMap Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Java programs that solve real problems with HashMap, LinkedHashMap and TreeMap. Write each one yourself first, then open the solution – every solution was compiled and run on Java 21, and the output shown is the real output.

Question 1 Easy

Count word frequencies in a paragraph (case-insensitive, ignoring punctuation) with a HashMap, then print the three most frequent words (ties broken alphabetically).

Question page & discussion
Question 2 Easy

Find the first character that appears only once in each string, using a LinkedHashMap so that counting keeps the original order.

Question page & discussion
Question 3 Medium

Group words that are anagrams of each other by using each word's sorted letters as a map key; print the groups with more than one word.

Question page & discussion
Question 4 Medium

Use a TreeMap from minimum percentage to grade and another from minimum percentage to scholarship amount; for each student look up both with floorEntry and print a report.

Question page & discussion
Question 5 Hard

Build a generic LRU cache of capacity 3 on top of LinkedHashMap that counts hits and misses; simulate loading user profiles and print the statistics and final contents.

Question page & discussion
Question 6 Medium

Store contacts in a TreeMap (case-insensitive names), support add/update and prefix search with subMap, and print the matches for a few prefixes.

Question page & discussion
Question 7 Medium

Parse log lines "ip path status", count requests per IP and errors (status ≥ 400) per IP, and print the IPs sorted by request count with their error counts.

Question page & discussion
Question 8 Easy

Combine stock from two warehouses into one TreeMap by adding quantities, list items present in only one warehouse, and print items whose total is below 20.

Question page & discussion
Question 9 Hard

Count the contiguous sub-arrays whose sum equals k in O(n) using a HashMap of prefix-sum frequencies; test with daily profits.

Question page & discussion
Question 10 Medium

Store a day's events in a TreeMap keyed by "HH:MM" time strings; print the agenda, the next event after 11:15, the last event before 14:00 and all events between 10:00 and 13:00.

Question page & discussion
All exercises doneBack to HashMap and TreeMapRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links