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

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

CQL: Find Median of a List

Cassandra CQL

Goal -- WPM

Ready
Exercise Algorithm Area
1CREATE OR REPLACE FUNCTION find_median_of_list(
2input_list list<int>
3)
4RETURNS double
5AS $$
6DECLARE
7n int;
8sorted_list list<int>;
9mid_index int;
10median double;
11BEGIN
12n = list_size(input_list);
13
14-- Handle empty list edge case
15IF n = 0 THEN
16RETURN NULL;
17END IF;
18
19-- Sort the list (CQL does not have a direct sort function for lists,
20-- so we simulate it by converting to a set and back, or assume pre-sorted if possible.
21-- For this example, we'll assume we can sort it or it's already sorted.
22-- In a real scenario, you'd need a more robust sorting mechanism or pre-processing.
23-- For demonstration, let's assume we have a way to get a sorted list.
24-- A common approach is to use a UDT and then sort that, or external processing.
25-- Here, we'll use a placeholder for sorting.
26-- sorted_list = sort_list(input_list); -- Placeholder for actual sorting
27-- For simplicity in this example, we'll assume the input list is already sorted.
28-- If not, this function would need a complex sorting implementation.
29sorted_list = input_list; -- ASSUMING INPUT IS ALREADY SORTED FOR THIS EXAMPLE
30
31-- Calculate median based on list size
32IF n % 2 = 1 THEN
33-- Odd number of elements
34mid_index = n / 2;
35median = to_double(sorted_list[mid_index]);
36ELSE
37-- Even number of elements
38mid_index = n / 2;
39median = (to_double(sorted_list[mid_index - 1]) + to_double(sorted_list[mid_index])) / 2.0;
40END IF;
41
42RETURN median;
43END; $$
44
45-- NOTE: The 'sort_list' function is a placeholder. CQL does not have a built-in
46-- list sorting function. For a real-world scenario, you would need to:
47-- 1. Implement a complex sorting algorithm in CQL (e.g., using UDTs and iterative swaps).
48-- 2. Pre-sort the data before inserting it into Cassandra.
49-- 3. Perform sorting outside Cassandra and then query the sorted results.
50-- This example assumes the input list is already sorted for demonstration purposes.
51
52-- Example of a conceptual sort_list (highly inefficient and complex in CQL):
53-- CREATE OR REPLACE FUNCTION sort_list(input_list list<int>)
54-- RETURNS list<int>
55-- AS $$
56-- DECLARE
57-- n int;
58-- temp_list list<int>;
59-- i int;
60-- j int;
61-- temp int;
62-- BEGIN
63-- n = list_size(input_list);
64-- temp_list = input_list;
65-- -- Bubble sort simulation (highly inefficient)
66-- FOR i = 0 TO n - 2 DO
67-- FOR j = 0 TO n - 2 - i DO
68-- IF temp_list[j] > temp_list[j+1] THEN
69-- temp = temp_list[j];
70-- temp_list[j] = temp_list[j+1];
71-- temp_list[j+1] = temp;
72-- END IF;
73-- END FOR;
74-- END FOR;
75-- RETURN temp_list;
76-- END; $$
77
78-- Example Usage:
79-- SELECT find_median_of_list([1, 2, 3, 4, 5]); -- Expected: 3.0
80-- SELECT find_median_of_list([1, 2, 3, 4]); -- Expected: 2.5
81-- SELECT find_median_of_list([]); -- Expected: NULL
Algorithm description viewbox

CQL: Find Median of a List

Algorithm description:

This Cassandra CQL function calculates the median of a list of integers. It handles lists with an odd number of elements by returning the middle element, and lists with an even number of elements by returning the average of the two middle elements. It also correctly returns NULL for an empty list. This is useful for statistical analysis of data stored in Cassandra.

Algorithm explanation:

The `find_median_of_list` function first determines the size of the input list. If the list is empty, it returns NULL. For non-empty lists, it calculates the median. If the list size `n` is odd, the median is the element at index `n / 2`. If `n` is even, the median is the average of the elements at indices `n / 2 - 1` and `n / 2`. The primary challenge in CQL is sorting the list, as there's no direct `sort` function. This example assumes the input list is pre-sorted or uses a placeholder for sorting. The time complexity is dominated by the sorting step, which, if implemented naively in CQL, could be O(N^2). If the list is pre-sorted, the median calculation is O(1). Space complexity is O(N) if a new sorted list is created, or O(1) if sorting is in-place (not typical in CQL UDFs). Edge cases handled: empty list, odd/even list lengths.

Pseudocode:

FUNCTION find_median_of_list(input_list):
  n = size of input_list

  IF n is 0 THEN
    RETURN NULL
  END IF

  -- Assume input_list is sorted. If not, sort it first.
  sorted_list = input_list

  IF n is odd THEN
    mid_index = n / 2
    RETURN sorted_list[mid_index]
  ELSE
    mid_index = n / 2
    RETURN (sorted_list[mid_index - 1] + sorted_list[mid_index]) / 2.0
  END IF
END FUNCTION