0% found this document useful (0 votes)
16 views

Viva Questions

Interview Bit best question on planet earth

Uploaded by

vikashmaurya7235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Viva Questions

Interview Bit best question on planet earth

Uploaded by

vikashmaurya7235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Anagram Checker

Problem: Write a program to check whether two strings are anagrams


of each other using a HashMap. Two strings are anagrams if they
contain the same characters with the same frequency.

Sample Input/Output:
Input: "listen", "silent"
Output: true

Input: "hello", "world"


Output: false

2. Count the Number of Distinct Elements in Every Window of


Size K

Problem: Given an array of integers and a window size k, use a


HashMap to print the count of distinct elements in every window of size
k.

Sample Input/Output:
Input: arr = [1, 2, 1, 3, 4, 2, 3], k = 4
Output: [3, 4, 4, 3]
Explanation:
Window [1, 2, 1, 3] has 3 distinct numbers.
Window [2, 1, 3, 4] has 4 distinct numbers.
...

Two Sum Problem using HashMap

Problem: Given an array of integers and a target sum, use a HashMap


to find if there are two numbers in the array that sum up to the target.
Return the indices of those numbers.

Sample Input/Output:
Input: arr = [2, 7, 11, 15], target = 9
Output: [0, 1] (since arr[0] + arr[1] = 2 + 7 = 9)
3. Employee Department Mapping

Problem: Write a Java program to store employee names and their


departments using a HashMap. Implement the following functionalities:

 Add a new employee and their department.


 Get the department of a specific employee.
 List all employees in a given department.

Sample Input/Output:
Input: Add Alice in HR, Bob in IT, Eve in IT
Output: Employees in IT: [Bob, Eve]

Student Ranking System

Problem: Implement a student ranking system where students and


their scores are stored in a HashMap. Write a function to:

 Add student names and their scores.


 Get the rank of a student (1st, 2nd, 3rd, etc.) based on their
score. Higher scores get a better rank.

Sample Input/Output:
Input: Alice - 85, Bob - 90, Charlie - 75
Output: Bob: 1st, Alice: 2nd, Charlie: 3rd

4. Intersection of Two Arrays using HashMap

Problem: Write a Java program to find the intersection of two arrays


using a HashMap. The result should contain all the common elements
between the two arrays.

Sample Input/Output:
Input: arr1 = [1, 2, 2, 3], arr2 = [2, 2, 4, 5]
Output: [2, 2]

5. Sort HashMap by Values


Problem: Write a Java program to sort a HashMap by values
(ascending or descending order) and print the sorted map.

Sample Input/Output:
Input: {Alice=85, Bob=90, Charlie=75}
Output: {Charlie=75, Alice=85, Bob=90} // sorted by value

6. Check if Two Maps are Identical

Problem: Write a Java program to check whether two HashMap


instances are identical in terms of keys and values.

Sample Input/Output:
Input: map1 = {Alice=85, Bob=90}, map2 = {Alice=85, Bob=90}
Output: true

Input: map1 = {Alice=85, Bob=90}, map2 = {Alice=85,


Charlie=90}
Output: false

7 Frequency of Characters

Problem: Write a Java program that takes a string as input and uses a
HashMap to count the frequency of each character in the string. Ignore
spaces.

Sample Input/Output:
Input: "hello world"
Output: {h=1, e=1, l=3, o=2, w=1, r=1, d=1}

8 Find Duplicates in an Array

Problem: Given an array of integers, use a HashMap to find and print


the duplicate elements.

Sample Input/Output:
Input: [1, 2, 3, 1, 4, 5, 2]
Output: [1, 2]
9 Word Count in a Sentence

Problem: Write a Java program that reads a sentence and uses a


HashMap to count the occurrences of each word in the sentence. Ignore
case sensitivity.

Sample Input/Output:
Input: "Java is fun and Java is powerful"
Output: {java=2, is=2, fun=1, and=1, powerful=1}

You might also like