Top 20 AMCAT Coding Questions with Java Solutions
1. First Repeating Element
Approach: Use a `HashSet` to track seen elements. The first element that is already present in the
set is the first repeating element.
2. Remove Duplicates
Approach: Convert the array to a `HashSet` to eliminate duplicates, then convert it back to an array
or use an `ArrayList` for dynamic sizing.
3. Find Common Elements in Two Arrays
Approach: Convert one array to a `HashSet` for constant-time lookups, then iterate through the
second array and collect elements that are present in the set.
4. Count Frequency of Elements in an Array
Approach: Use a `HashMap` where the key is the element and the value is its frequency count.
Iterate through the array, updating the map accordingly.
5. Find the Most Frequent Element in an Array
Approach: First, build a frequency map using a `HashMap`. Then, iterate through the map to find the
key with the highest frequency.
6. Find All Pairs with a Given Sum
Approach: Use a `HashSet` to store elements. For each element `num`, check if `target - num`
exists in the set. If it does, you've found a pair.
7. Check if Two Arrays are Equal
Approach: Use two `HashMap` instances to count frequencies of elements in both arrays, then
compare the maps for equality.
8. Find Missing Numbers in a Range
Approach: Create a `HashSet` of the array elements, then iterate from 1 to N and collect numbers
not present in the set.
9. Find the Union and Intersection of Two Arrays
Approach: For union, add all elements to a `HashSet`. For intersection, add elements of one array to
a set, then iterate through the second array and collect elements that are also in the set.
10. Find the Second Largest Element in an Array
Approach: Use a `TreeSet` to store elements in sorted order, then retrieve the second last element.
Alternatively, iterate through the array while keeping track of the largest and second largest
elements.
11. Reverse an Array Without Using an Additional Array
Approach: Swap elements from the start and end moving towards the center.
12. Find Continuous Subarray with Given Sum
Approach: Use a sliding window approach or a `HashMap` to track cumulative sums.
13. Separate Zeros from Non-Zeros in an Array
Approach: Iterate through the array, moving non-zero elements forward, and fill the remaining
positions with zeros.
14. Find All Leaders in an Array
Approach: Traverse the array from the end, keeping track of the maximum element found so far.
Elements greater than this maximum are leaders.
15. Convert an Array to ArrayList and Vice Versa
Approach: Use `Arrays.asList()` to convert to `ArrayList` and `list.toArray()` to convert back to an
array.
16. Check if an Array is a Palindrome
Approach: Compare elements from the start and end moving towards the center.
17. Find the Intersection of Multiple Arrays
Approach: Use a `HashMap` to count occurrences across arrays and identify common elements.
18. Find the Missing Number in a Sequence
Approach: Calculate the expected sum of numbers from 1 to N and subtract the actual sum of array
elements.
19. Count Occurrences of Each Element in an Array
Approach: Use a `HashMap` to store the frequency of each element.
20. Find the Smallest and Largest Elements in an Array
Approach: Iterate through the array, updating the minimum and maximum values accordingly.