Get Most Cookie Used
This is a real interview challenge I've seen. You get a CSV log of cookies with timestamps. Find the most active cookie for a given date.
21 Mar 2024

This is a real interview challenge I've seen. You get a CSV log of cookies with timestamps. Find the most active cookie for a given date.
The problem
Given a log file:
cookie,timestamp
AtY0laUfhglK3lC7,2018-12-09T14:19:00+00:00
SAZuXPGUrfbcn5UA,2018-12-09T10:13:00+00:00
5UAVanZf6UtGyKVS,2018-12-09T07:25:00+00:00
AtY0laUfhglK3lC7,2018-12-09T06:19:00+00:00
SAZuXPGUrfbcn5UA,2018-12-08T22:03:00+00:00
4sMM2LxV07bPJzwf,2018-12-08T21:30:00+00:00
fbcn5UAVanZf6UtG,2018-12-08T09:30:00+00:00
4sMM2LxV07bPJzwf,2018-12-07T23:30:00+00:00
Build a CLI that takes -f for the filename and -d for the date, then returns the most active cookie(s) for that day. If there's a tie, return all of them.
Key constraints
- The log is sorted by timestamp (newest first).
- You can assume the file fits in memory.
- No data-wrangling libraries — they want to see your code.
The approach
Since the data is sorted by timestamp, I can use binary search to find the first occurrence of the target date. Then scan forward to collect all entries for that date and count them.
Finding the date range
function binarySearchFirstOccurrence(arr, targetDate) {
let left = 0;
let right = arr.length - 1;
let result = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const entryDate = arr[mid].timestamp.split('T')[0];
if (entryDate === targetDate) {
result = mid;
right = mid - 1;
} else if (entryDate > targetDate) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
Counting and finding the winner
function getMostActiveCookie(entries, targetDate) {
const startIdx = binarySearchFirstOccurrence(entries, targetDate);
if (startIdx === -1) return [];
const counts = new Map();
let maxCount = 0;
for (let i = startIdx; i < entries.length; i++) {
const entryDate = entries[i].timestamp.split('T')[0];
if (entryDate !== targetDate) break;
const cookie = entries[i].cookie;
const count = (counts.get(cookie) || 0) + 1;
counts.set(cookie, count);
maxCount = Math.max(maxCount, count);
}
return [...counts.entries()]
.filter(([_, count]) => count === maxCount)
.map(([cookie]) => cookie);
}
Complexity
- Time: O(log n) for binary search + O(k) for scanning entries on that date, where k is the number of entries for the target day.
- Space: O(k) for the counts map.
Trade-offs
Binary search exploits the sorted order — smart. A linear scan would also work and is simpler, but O(n) vs O(log n + k) matters for large files. The real signal in this interview question isn't the algorithm — it's clean code, proper testing, and edge case handling (empty files, dates with no entries, multiple winners).
Production-Ready Systems with LLMs and Agents
A live Maven cohort, 5 October to 2 November: eight 90-minute sessions where you build LLM and agent systems that survive real traffic, real cost and real failure. Tuesdays and Thursdays, 7:30 to 9:00pm London.
Cohort 2 starts 5 October. Eight live sessions, $1,500.
View the live cohortKeep reading
- The Algorithms Worth Knowing in Code Review: Cutting Big O Without Being Clever
- Tracking Down the Mystery: Finding the Unique Delivery ID
- Elusive Least Occurring Number in an Array
- Solving the Height of a Binary Tree Puzzle
- Harry Potter Discounts: A Code Challenge Algorithm
- Implementing a Task Heap Service in TypeScript