ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

KQL: Implement Binary Search on a Sorted Array

Kusto Query Language (KQL)

Goal -- WPM

Ready
Exercise Algorithm Area
1let binarySearch = (sortedArray: dynamic, target: int) -> int {
2let n = array_length(sortedArray);
3if (n == 0) {
4return -1; // Target not found in empty array
5}
6
7let low = 0;
8let high = n - 1;
9
10while (low <= high) {
11// Calculate mid point to avoid potential overflow
12let mid = low + floor((high - low) / 2);
13let midValue = sortedArray[mid];
14
15if (midValue == target) {
16return mid; // Target found at mid index
17} else if (midValue < target) {
18// Target is in the right half
19low = mid + 1;
20} else {
21// Target is in the left half
22high = mid - 1;
23}
24}
25
26// Target not found in the array
27return -1;
28};
29
30// Example Usage:
31let data = dynamic([2, 5, 8, 12, 16, 23, 38, 56, 72, 91]);
32print "Index of 23: ", binarySearch(data, 23);
33print "Index of 5: ", binarySearch(data, 5);
34print "Index of 91: ", binarySearch(data, 91);
35print "Index of 1: ", binarySearch(data, 1);
36print "Index of 100: ", binarySearch(data, 100);
37print "Index of 56 in empty array: ", binarySearch(dynamic([]), 56);
Algorithm description viewbox

KQL: Implement Binary Search on a Sorted Array

Algorithm description:

This KQL function implements the binary search algorithm to efficiently find the index of a target value within a sorted array. Binary search is a highly optimized search algorithm that repeatedly divides the search interval in half. It's crucial for scenarios involving large datasets where quick lookups are essential, such as searching for a specific record in a sorted database index or finding a configuration setting in a sorted list of parameters.

Algorithm explanation:

The `binarySearch` function takes a sorted dynamic array and a target integer. It initializes `low` to the first index (0) and `high` to the last index (`n-1`). The algorithm iteratively narrows down the search space. In each iteration, it calculates the middle index `mid`. If the value at `mid` matches the target, its index is returned. If the value at `mid` is less than the target, the search continues in the right half by setting `low` to `mid + 1`. Otherwise, the search continues in the left half by setting `high` to `mid - 1`. If the loop finishes without finding the target (i.e., `low` becomes greater than `high`), it returns -1. Edge cases handled include an empty array, the target not being present, and the target being the first or last element. The time complexity is O(log N) due to the halving of the search space in each step. The space complexity is O(1) as it only uses a few variables.

Pseudocode:

function binarySearch(sortedArray, target):
  n = length of sortedArray
  if n is 0:
    return -1

  low = 0
  high = n - 1

  while low <= high:
    mid = low + floor((high - low) / 2)
    midValue = sortedArray[mid]

    if midValue == target:
      return mid
    else if midValue < target:
      low = mid + 1
    else:
      high = mid - 1

  return -1