Monday, May 19, 2025

HASHSET Vs LINKEDHASHSET - USE CASES TO SOLVE

Use Case 1: Removing Duplicates in User Inputs

πŸ“Œ Problem: You’re developing survey application where users enter their favorite programming languages. You want to store only unique languages.


Use Case 2: Maintaining Recent Search History (Without Duplicates)

πŸ“Œ Problem: Build feature that stores the recent search terms user enters, preserving the order they searched them, but preventing repeated terms.


Use Case 3: Display Sorted List of Student Names

πŸ“Œ Problem: You want to display sorted list of student names in alphabetical order automatically.

4. Task Scheduling System (Using Queue & Stack)

Use CaseMaintain order of tasks (Queue for FIFO), redo/undo (Stack for LIFO).


PROJECTS

1. Caching System – HashMap

Problem:
You are designing search system. To reduce database calls, you need to implement caching mechanism that stores search results based on keywords.

Question:

Implement method getSearchResult(String keyword) that first checks the cache. If the result is not cached, simulate fetching from DB and store it in the cache.

➀ Enhance it to store max of items. When new item is added beyond this limit, remove the oldest entry (Use LinkedHashMap if necessary).

 


2. Unique Visitor Tracker – TreeSet

Problem:
Your website records user names visiting page. Visitors can come multiple times, but you only want to display unique and alphabetically sorted names.

Question:

Given an array of visitor names (including duplicates), store and display the unique visitors in ascending order.

➀ Modify it to ignore case sensitivity (e.g., "John" and "john" should be treated as the same).

 


3. Leaderboard System – TreeMap

Problem:
Create leaderboard that maps scores to player names. If two players have the same score, store both.

Question:

Write program that accepts player names and scores from the user. Display the leaderboard sorted in descending order of scores.

➀ Allow multiple players to have the same score (hint: use TreeMap<Integer, List<String>>).

 


4. Product Order Frequency – HashMap

Problem:
You're tracking the frequency of products ordered in an e-commerce store.

Question:

Given list of products ordered, write program to display how many times each product was ordered.

➀ Modify the solution to sort the products by frequency (hint: use custom comparator with list).

 


5. Undo/Redo System – Stack

Problem:
You're building text editor with Undo and Redo functionality. Every text change should be recorded.

Question:

Implement two operations:

  • undo() β†’ removes the last action and stores it in redo stack

  • redo() β†’ restores the last undone action

➀ Allow user input for actions and simulate text editor with options like:

  • Type text

  • Undo

  • Redo

  • Show current text



No comments:

Post a Comment