Hone logo
Hone
Problems

Type-Level Quick Sort in TypeScript

This challenge asks you to implement a type-level quick sort algorithm in TypeScript. Type-level programming allows us to perform computations and manipulations on types themselves, rather than values at runtime. This exercise will test your understanding of conditional types, mapped types, and recursion in TypeScript.

Problem Description

The goal is to create a type QuickSort<T> that takes a tuple type T as input and returns a new tuple type with the elements of T sorted in ascending order. You'll need to define helper types to handle partitioning, selecting a pivot, and recursively sorting the sub-tuples. The solution should leverage TypeScript's type system to perform the sorting logic entirely at compile time.

Key Requirements:

  • Input: A tuple type T.
  • Output: A new tuple type with the elements of T sorted in ascending order.
  • Pivot Selection: You can choose any strategy for selecting the pivot (e.g., the first element, the middle element, a random element). For simplicity, let's use the first element as the pivot.
  • Partitioning: Create two new tuple types: one containing elements less than the pivot, and one containing elements greater than or equal to the pivot.
  • Recursion: Recursively apply QuickSort to the "less than" and "greater than or equal to" tuples.
  • Concatenation: Combine the sorted "less than" tuple, the pivot (as a single-element tuple), and the sorted "greater than or equal to" tuple to produce the final sorted tuple.

Expected Behavior:

The type QuickSort<[1, 5, 2, 4, 3]> should resolve to a type equivalent to [1, 2, 3, 4, 5]. The type system should enforce the sorted order.

Edge Cases to Consider:

  • Empty Tuple: QuickSort<[]> should resolve to [].
  • Tuple with One Element: QuickSort<[5]> should resolve to [5].
  • Tuple with Duplicate Elements: The algorithm should handle duplicate elements correctly (e.g., QuickSort<[3, 1, 3, 2]> should resolve to [1, 2, 3, 3]).
  • Tuple with Non-Numeric Elements: While the examples focus on numbers, the type system should ideally allow for sorting tuples of any comparable types (e.g., strings). For this challenge, focus on numeric types.

Examples

Example 1:

Input: [1, 5, 2, 4, 3]
Output: [1, 2, 3, 4, 5]
Explanation: The elements are sorted in ascending order.

Example 2:

Input: [5, 2, 8, 1, 9, 4]
Output: [1, 2, 4, 5, 8, 9]
Explanation: The elements are sorted in ascending order.

Example 3:

Input: [3, 1, 3, 2]
Output: [1, 2, 3, 3]
Explanation: Duplicate elements are handled correctly.

Constraints

  • The input tuple T will contain only numbers.
  • The algorithm must be implemented using TypeScript's type system (no runtime code).
  • The solution should be reasonably efficient in terms of type complexity. Excessively complex or deeply recursive type definitions may lead to compilation errors.
  • The pivot selection strategy must be consistent (using the first element).

Notes

  • This is a challenging problem that requires a good understanding of TypeScript's advanced type features.
  • Consider breaking down the problem into smaller, more manageable helper types.
  • You'll likely need to use conditional types, mapped types, and recursion extensively.
  • Debugging type-level code can be difficult. Start with simple cases and gradually increase the complexity.
  • The infer keyword can be helpful for extracting type information.
  • Think about how to represent "less than" and "greater than or equal to" relationships at the type level. You might need to use a helper type to compare two numbers and produce a boolean type.
Loading editor...
typescript