cpu.mode fastest code on the internet
01 specification

NumKong Sparse Dot Product

Implement NumKong's u32-indexed sparse f32 dot product. The judge builds your source as libsolution.so and calls the exported function from a verifier executable.

The required symbol is extern "C" void nk_sparse_dot_u32f32(const uint32_t *a, const uint32_t *b, const float *a_weights, const float *b_weights, size_t a_length, size_t b_length, double *product).

Inputs are sorted ascending arrays of unique u32 indices. The result is the f64 accumulation of a_weights[i] * b_weights[j] for matching indices. The verifier checks every generated case, not just an aggregate.

The generated input includes explicit corner cases, NumKong-style equal-length random sparse vectors, skewed posting-list style intersections, and larger randomized sparse dots.

minimal Rust solution
#[unsafe(no_mangle)]
pub unsafe extern "C" fn nk_sparse_dot_u32f32(
    a: *const u32,
    b: *const u32,
    a_weights: *const f32,
    b_weights: *const f32,
    a_length: usize,
    b_length: usize,
    product: *mut f64,
) {
    let a = unsafe { std::slice::from_raw_parts(a, a_length) };
    let b = unsafe { std::slice::from_raw_parts(b, b_length) };
    let a_weights = unsafe { std::slice::from_raw_parts(a_weights, a_length) };
    let b_weights = unsafe { std::slice::from_raw_parts(b_weights, b_length) };

    let mut i = 0;
    let mut j = 0;
    let mut sum = 0.0_f64;

    while i < a.len() && j < b.len() {
        let ai = a[i];
        let bj = b[j];
        if ai == bj {
            sum += f64::from(a_weights[i]) * f64::from(b_weights[j]);
            i += 1;
            j += 1;
        } else if ai < bj {
            i += 1;
        } else {
            j += 1;
        }
    }

    unsafe {
        *product = sum;
    }
}
02 scope / runtime over time
Lang
System
double-click zooms out
03 leaderboard
Leaderboard · top 8 click any row to expand · open multiple to compare
Rank User Lang Best Position in CDF Analysis When
04 submit
Your Solution
Single File
Sign in to submit.