Monday, May 19, 2025

HASHSET Vs LINKEDHASHSET - USE CASES TO SOLVE

Use Case 1: Removing Duplicates in User Inputs

📌 Problem: You’re developing a 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 a feature that stores the recent search terms a 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 a sorted list of student names in alphabetical order automatically.

4. Task Scheduling System (Using Queue & Stack)

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


PROJECTS

1. Caching System – HashMap

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

Question:

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

➤ Enhance it to store a max of 5 items. When a 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 a 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 a leaderboard that maps scores to player names. If two players have the same score, store both.

Question:

Write a 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 a list of products ordered, write a program to display how many times each product was ordered.

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

 


5. Undo/Redo System – Stack

Problem:
You're building a 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 a redo stack

  • redo() → restores the last undone action

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

  • Type text

  • Undo

  • Redo

  • Show current text



No comments:

Post a Comment