Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the first occurrence index of the target in the sorted array.
DSA Java
int firstOccurrence(int[] arr, int target) {
int low = 0, high = arr.length - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
result = mid;
high = mid [1] 1;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes infinite loop or wrong index.
Not updating high pointer after finding target.
✗ Incorrect
When we find the target, we move the high pointer to mid - 1 to continue searching on the left side for the first occurrence.
2fill in blank
mediumComplete the code to find the last occurrence index of the target in the sorted array.
DSA Java
int lastOccurrence(int[] arr, int target) {
int low = 0, high = arr.length - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target) {
result = mid;
low = mid [1] 1;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes infinite loop or wrong index.
Not updating low pointer after finding target.
✗ Incorrect
When the target is found, move the low pointer to mid + 1 to search the right side for the last occurrence.
3fill in blank
hardFix the error in the countOccurrences method to correctly calculate the count of target in the array.
DSA Java
int countOccurrences(int[] arr, int target) {
int first = firstOccurrence(arr, target);
if (first == -1) {
return 0;
}
int last = lastOccurrence(arr, target);
return last [1] first + 1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes wrong count.
Forgetting to add 1 after subtraction.
✗ Incorrect
Count is last index minus first index plus one, so use subtraction between last and first.
4fill in blank
hardFill both blanks to create a map of each unique element to its count in the sorted array.
DSA Java
Map<Integer, Integer> countMap = new HashMap<>(); for (int num : arr) { countMap.put(num, countMap.getOrDefault(num, [1]) [2] 1); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as default causes counts to be off by one.
Using '-' instead of '+' causes wrong counts.
✗ Incorrect
Use 0 as default count and add 1 for each occurrence.
5fill in blank
hardFill both blanks to create a method that returns a map of elements to their counts using binary search methods.
DSA Java
Map<Integer, Integer> countOccurrencesMap(int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
Set<Integer> unique = new HashSet<>();
for (int num : arr) {
if (!unique.contains(num)) {
unique.add(num);
int count = lastOccurrence(arr, num) [1] firstOccurrence(arr, num) [2] 1;
map.put(num, count);
}
}
return map;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for first operation.
Forgetting to add 1 after subtraction.
✗ Incorrect
Count is last minus first plus one; the plus one is added after subtraction.
