Bitwise User Permissions Analysis - Problem
You work for a company that manages user permissions through a sophisticated bitwise permission system. Each user has permissions encoded as an integer where each bit represents access to a specific feature or resource.
Given a table of user permissions, you need to analyze the permission patterns across all users:
- Common Permissions: Features that all users can access (bitwise AND of all permissions)
- Any Permissions: Features that at least one user can access (bitwise OR of all permissions)
For example, if User A has permissions 5 (binary: 0101) and User B has permissions 3 (binary: 0011):
- Common permissions:
5 & 3 = 1 (binary: 0001)- only the rightmost feature is accessible to both - Any permissions:
5 | 3 = 7 (binary: 0111)- three different features are accessible by at least one user
Goal: Write a SQL query to calculate both metrics for all users in the system.
Input & Output
example_1.sql โ Basic Case
$
Input:
user_permissions table:
+---------+-------------+
| user_id | permissions |
+---------+-------------+
| 1 | 5 |
| 2 | 12 |
| 3 | 7 |
| 4 | 3 |
+---------+-------------+
โบ
Output:
+--------------+----------+
| common_perms | any_perms|
+--------------+----------+
| 0 | 15 |
+--------------+----------+
๐ก Note:
Binary analysis: 5(0101) & 12(1100) & 7(0111) & 3(0011) = 0(0000). For OR: 5(0101) | 12(1100) | 7(0111) | 3(0011) = 15(1111). No permissions are common to all users, but all 4 bit positions are used by at least one user.
example_2.sql โ All Same Permissions
$
Input:
user_permissions table:
+---------+-------------+
| user_id | permissions |
+---------+-------------+
| 1 | 7 |
| 2 | 7 |
| 3 | 7 |
+---------+-------------+
โบ
Output:
+--------------+----------+
| common_perms | any_perms|
+--------------+----------+
| 7 | 7 |
+--------------+----------+
๐ก Note:
When all users have identical permissions (7 = 0111), both common_perms and any_perms equal 7. All users share the same three permissions.
example_3.sql โ Single User Edge Case
$
Input:
user_permissions table:
+---------+-------------+
| user_id | permissions |
+---------+-------------+
| 1 | 15 |
+---------+-------------+
โบ
Output:
+--------------+----------+
| common_perms | any_perms|
+--------------+----------+
| 15 | 15 |
+--------------+----------+
๐ก Note:
With only one user having permissions 15(1111), both common and any permissions equal 15, as this single user defines both the intersection and union of permissions.
Visualization
Tap to expand
Understanding the Visualization
1
User Permissions
Each user has a binary permission pattern
2
Find Common Access
Use AND operation to find areas accessible to ALL users
3
Find Any Access
Use OR operation to find areas accessible to ANY user
4
Security Analysis
Compare common vs any permissions for access control insights
Key Takeaway
๐ฏ Key Insight: BIT_AND finds the intersection (common to all), while BIT_OR finds the union (available to anyone). This analysis is crucial for security auditing and access control management.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all records using optimized aggregate functions
โ Linear Growth
Space Complexity
O(1)
Only storing final aggregated results
โ Linear Space
Constraints
- 1 โค number of users โค 105
- 0 โค permissions โค 231 - 1
- user_id is the primary key
- At least one user exists in the table
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code