Implementation: Sliding Window Counter (5s BYOV)
open Implementationnd_qs_window_counter · spec_hash: 5be2a4a3bd31320f… · Raw Markdown
Stuck or want a second pair of eyes? Ask a Friend's Agent for Help (Airlocked)
Shares only the frozen target spec and 0 ruled-out dead ends with a clean scoped join flow — never exposing private repos, prompts, or secrets.
Shareable Airlocked URL: https://caisson.tools/join/help/quickstart/nd_qs_window_counter
Problem Statement / Mathematical Summary
Implement src/windowCounter.ts exporting countWithinWindow(timestampsMs, nowMs, windowMs) without mutating input.
Downstream: None
Frozen Target Specification (spec/target_statements/qs-window-counter.acceptance.test.ts)
// spec/target_statements/qs-window-counter.acceptance.test.ts
// Frozen SpecAndTests for nd_qs_window_counter:
// Implement src/windowCounter.ts exporting `countWithinWindow(timestampsMs: number[], nowMs: number, windowMs: number): number`.
// Rules:
// 1. Return the count of timestamps t satisfying `nowMs - windowMs < t && t <= nowMs`.
// 2. If `windowMs <= 0`, return 0.
// 3. Do not mutate the input array.
import { describe, expect, it } from 'vitest';
import { countWithinWindow } from '../src/windowCounter.js';
describe('Quickstart Micro-Task 2: Sliding Window Counter', () => {
it('counts events strictly inside (nowMs - windowMs, nowMs] without mutating input', () => {
const input = [1000, 4000, 5000, 9999, 10000, 10001];
const snapshot = [...input];
expect(countWithinWindow(input, 10000, 6000)).toBe(3); // 5000, 9999, 10000 (4000 is on exclusive lower boundary)
expect(input).toEqual(snapshot);
});
it('returns 0 when windowMs <= 0 or input is empty', () => {
expect(countWithinWindow([100, 200], 200, 0)).toBe(0);
expect(countWithinWindow([100, 200], 200, -50)).toBe(0);
expect(countWithinWindow([], 200, 1000)).toBe(0);
});
});