Identify read-only files from permission records
Jump TradingSoftware Engineer · Online AssessmentLast updated
This problem tests your ability to parse Unix-style permission strings and implement filtering logic based on bitmask-like constraints.
Problem Statement
You are given a list of file metadata entries, where each entry is a string formatted as "permissions filename". The permissions portion is a 9-character string representing Unix-style file permissions (e.g., "rwxr-xr-x").
The permission string is divided into three segments of three characters each:
- User (the first 3 characters)
- Group (the middle 3 characters)
- Others (the last 3 characters)
In each segment, the character at index 0 represents 'read' (r), index 1 represents 'write' (w), and index 2 represents 'execute' (x). If a permission is missing, it is represented by a hyphen (-).
A file is considered read-only if it has no 'write' (w) permissions for the user, the group, OR others. In other words, for a file to be read-only, all three write-position characters must be hyphens ('-').
Implement the function find_read_only_files(file_list) that returns a list of filenames that are read-only, sorted alphabetically.
Constraints
file_listcontains between 1 and 1000 strings.- Each filename consists of alphanumeric characters.
- Each permission string is exactly 9 characters long.
Examples
Example 1:
Input: ["r-xr-xr-x file1", "rw-r--r-- file2", "r--r--r-- file3"]
Output: ["file1", "file3"]
Explanation:
file1(r-xr-xr-x): Write positions are all '-', so it is read-only.file2(rw-r--r--): User has write access, so it is not read-only.file3(r--r--r--): Write positions are all '-', so it is read-only.
Function Signature
def find_read_only_files(file_list: list[str]) -> list[str]:
pass
Hints
Hint 1
The write permissions are located at indices 1, 4, and 7 of the 9-character permission string.
Hint 2
A file is read-only if the characters at indices 1, 4, and 7 are all equal to the hyphen character '-'.
Hint 3
Split each string by the space character to separate the permissions from the filename, then apply the condition.
Follow-up questions
- How would you modify your solution to identify files where ONLY the owner has write permission?
- If the input was a stream of millions of files, how would you optimize the memory usage?
- How would you handle potential malformed input strings where the permission segment is not exactly 9 characters?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.