Rate Limiter (Fixed Window)
Allow at most N requests per fixed time window. Given request timestamps, output 1 (allowed) or 0 (denied) for each — the core of API rate limiting.
Sample input
limit = 3, window = 1000ms, requests = [0,200,400,600,1100,1200]
Sample output
1 1 1 0 1 1
Solution
requests = [0, 200, 400, 600, 1100, 1200]
limit = 3
window_size = 1000
counts = {}
result = []
for t in requests:
w = t // window_size
counts[w] = counts.get(w, 0) + 1
result.append("1" if counts[w] <= limit else "0")
print(" ".join(result))
const requests = [0, 200, 400, 600, 1100, 1200];
const limit = 3, windowSize = 1000;
const counts = {};
const result = [];
for (const t of requests) {
const w = Math.floor(t / windowSize);
counts[w] = (counts[w] || 0) + 1;
result.push(counts[w] <= limit ? "1" : "0");
}
console.log(result.join(" "));
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
int[] requests = {0, 200, 400, 600, 1100, 1200};
int limit = 3, windowSize = 1000;
Map<Integer, Integer> counts = new HashMap<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < requests.length; i++) {
int w = requests[i] / windowSize;
int c = counts.merge(w, 1, Integer::sum);
sb.append(c <= limit ? "1" : "0");
if (i < requests.length - 1) sb.append(" ");
}
System.out.println(sb.toString());
}
}
fun main() {
val requests = intArrayOf(0, 200, 400, 600, 1100, 1200)
val limit = 3
val windowSize = 1000
val counts = HashMap<Int, Int>()
val result = requests.map { t ->
val w = t / windowSize
val c = counts.getOrDefault(w, 0) + 1
counts[w] = c
if (c <= limit) "1" else "0"
}
println(result.joinToString(" "))
}
let requests = [0, 200, 400, 600, 1100, 1200]
let limit = 3, windowSize = 1000
var counts = [Int: Int]()
var result = [String]()
for t in requests {
let w = t / windowSize
counts[w, default: 0] += 1
result.append(counts[w]! <= limit ? "1" : "0")
}
print(result.joined(separator: " "))
void main() {
final requests = [0, 200, 400, 600, 1100, 1200];
const limit = 3, windowSize = 1000;
final counts = <int, int>{};
final result = <String>[];
for (final t in requests) {
final w = t ~/ windowSize;
counts[w] = (counts[w] ?? 0) + 1;
result.add(counts[w]! <= limit ? '1' : '0');
}
print(result.join(' '));
}
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int main() {
vector<int> requests = {0, 200, 400, 600, 1100, 1200};
int limit = 3, windowSize = 1000;
unordered_map<int, int> counts;
bool first = true;
for (int t : requests) {
int w = t / windowSize;
counts[w]++;
if (!first) cout << " ";
cout << (counts[w] <= limit ? "1" : "0");
first = false;
}
cout << endl;
return 0;
}
#include <stdio.h>
int main() {
int requests[] = {0, 200, 400, 600, 1100, 1200};
int n = 6, limit = 3, windowSize = 1000, first = 1;
for (int i = 0; i < n; i++) {
int w = requests[i] / windowSize;
int count = 0;
for (int j = 0; j <= i; j++) {
if (requests[j] / windowSize == w) count++;
}
if (!first) printf(" ");
printf("%s", count <= limit ? "1" : "0");
first = 0;
}
printf("\n");
return 0;
}