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
๐Ÿ”‘ Bitwise Permission AnalysisUser Permissions (Binary Representation)User 10101= 5User 20011= 12User 31110= 7User 41100= 3Bitwise OperationsCommon Permissions (AND):0000= 0No permissions shared by ALL usersAny Permissions (OR):1111= 15All 4 permission types used by SOMEONESQL Query:SELECT BIT_AND(permissions) AS common_perms, BIT_OR(permissions) AS any_perms FROM user_permissions;
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing final aggregated results

n
2n
โœ“ 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
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 25
28.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen