Apply Simplified Gitignore Rules to a Workspace File List

Last updated: November 29, 2026

Quick Overview

Given gitignore-style patterns (comments, negation with !, trailing slash for directories, leading or middle slash for anchoring, *, ?, and **) and a list of file paths, return the files that are ignored, using last-match-wins and the rule that files inside an ignored directory cannot be re-included.

Replit
Coding & Algorithms
Software Engineer
Replit
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given gitignore-style patterns (comments, negation with !, trailing slash for directories, leading or middle slash for anchoring, *, ?, and **) and a list of file paths, return the files that are ignored, using last-match-wins and the rule that files inside an ignored directory cannot be re-included.

A Repl's file tree, search, and Agent context all need to skip node_modules, build output, and other ignored paths, so this matches real workspace code. The interviewer checks careful pattern semantics, translating globs to a matcher, and the subtle parent directory rule.

What the Interviewer Expects
  • Parses comments, blank lines, negation, and directory-only rules
  • Distinguishes anchored patterns from patterns that match at any depth
  • Translates *, ?, and ** correctly with respect to path separators
  • Applies rules in order with last match winning
  • Checks ancestor directories so ignored directories exclude all their contents
Key Topics to Cover
glob matching
regex
path semantics
precedence rules
file trees
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you make this fast for 200,000 files and 500 patterns?
  • How would you support nested .gitignore files in subdirectories?
  • Why can git not re-include a file whose parent directory is excluded?
  • How would you handle escaped characters such as a literal # or !?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Supported: # comments, ! negation, trailing / (matches directories only), a slash at the start or middle anchors to the root, * and ? stay within one path segment, **/ spans zero or more d...

High-Level Design

Compile each pattern into (regex, negate, dir_only). An unanchored pattern gets the prefix (?:.*/)? so *.log matches at any depth. To decide a file, first check each ancestor directory: if any i...


Submit Your Answer
Markdown supported

Related Questions