Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

Welcome to the Hex Web Development Interview! During the interview session, you'll use this boilerplate repository to build out a frontend application with your interviewer. Before the session starts, please clone this repository and run `npm install` and `uv sync` in the root to install dependencies. You can also add any other dependencies that you think that you may want to use, but there is no need to write any code or do anything else ahead of time to prepare for the session.

## Interview

During the interview session, you'll implement two functions in this repository to fix an application. All changes will be made to [`api/pagination.py`](api/pagination.py).

## Running the app

1) Install JS dependencies with `npm install`.
Expand Down
28 changes: 28 additions & 0 deletions api/busy_loop_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time


class BusyLoopDetector:
"""
Detects busy loops by tracking the number of calls within a time window.
If the number of calls exceeds the threshold within the window, it raises
an error to prevent infinite loops from consuming resources.
"""

def __init__(self, threshold: int = 1000, time_window: float = 1.0):
self._counter = 0
self._threshold = threshold
self._time_window = time_window
self._start_time = time.monotonic()

def _reset_timer(self) -> None:
self._start_time = time.monotonic()
self._counter = 0
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated

def check(self) -> None:
self._counter += 1
current_time = time.monotonic()

if current_time - self._start_time > self._time_window:
self._reset_timer()
elif self._counter > self._threshold:
raise RuntimeError("Busy loop detected")
4 changes: 4 additions & 0 deletions api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from typing import List, Optional
from models import Project
from busy_loop_detector import BusyLoopDetector


class ProjectDatabase:
Expand All @@ -11,6 +12,7 @@ def __init__(self):
raw_data = json.load(f)
# Convert raw dictionaries to Project objects
self._items = [Project(**item) for item in raw_data]
self._detector = BusyLoopDetector()
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated

def get_items(
self, page_size: int = 10, start_after: Optional[Project] = None
Expand All @@ -26,6 +28,8 @@ def get_items(
Returns:
List of Project objects for the requested page
"""
self._detector.check()

if start_after is None:
return self._items[:page_size]

Expand Down
Loading