Implement a File Change Watcher with Debouncing
Last updated: May 21, 2025
Quick Overview
Build a file system watcher that detects changes to files in a project directory and triggers callbacks with debouncing. Handle rapid successive changes, file creation and deletion, and recursive directory watching.
Cursor
May 21, 202515
8
2,166 solved
Build a file system watcher that detects changes to files in a project directory and triggers callbacks with debouncing. Handle rapid successive changes, file creation and deletion, and recursive directory watching.
Cursor needs to detect file changes to update its codebase index, refresh diagnostics, and trigger re-indexing. This question tests your understanding of event-driven systems and debouncing, which are fundamental patterns in editor development.
What the Interviewer Expects
- Implement a watcher that detects file create, modify, and delete events
- Add debouncing so rapid changes are batched into a single callback
- Handle recursive directory watching
- Manage watcher lifecycle with proper cleanup
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you handle a directory with 100,000 files without exhausting file descriptors?
- How would you handle the case where a file is renamed?
- How would you integrate this with the semantic indexer to trigger re-indexing?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
The problem requires us to build a file change watcher that monitors a directory for file creation, modification, and deletion events while implementing debouncing. The key pattern here is an **event-...
Approach
- Set up the watcher: Use a library like
watchdogin Python to monitor the file system for changes. The watcher should be configured to detectCreated,Modified, andDeletedevents. - **...