Table: Genders
+-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | gender | varchar | +-------------+---------+ user_id is the primary key (column with unique values) for this table. gender is ENUM (category) of type 'female', 'male', or 'other'. Each row in this table contains the ID of a user and their gender. The table has an equal number of 'female', 'male', and 'other'.
Write a solution to rearrange the Genders table such that the rows alternate between 'female', 'other', and 'male' in order. The table should be rearranged such that the IDs of each gender are sorted in ascending order.
Return the result table in the mentioned order.
The result format is shown in the following example.
Example 1:
Input: Genders table: +---------+--------+ | user_id | gender | +---------+--------+ | 4 | male | | 7 | female | | 2 | other | | 5 | male | | 3 | female | | 8 | male | | 6 | other | | 1 | other | | 9 | female | +---------+--------+ Output: +---------+--------+ | user_id | gender | +---------+--------+ | 3 | female | | 1 | other | | 4 | male | | 7 | female | | 2 | other | | 5 | male | | 9 | female | | 6 | other | | 8 | male | +---------+--------+ Explanation: Female gender: IDs 3, 7, and 9. Other gender: IDs 1, 2, and 6. Male gender: IDs 4, 5, and 8. We arrange the table alternating between 'female', 'other', and 'male'. Note that the IDs of each gender are sorted in ascending order.
Problem Overview: You are given a table where each row contains a user_id and a gender. The goal is to rearrange the rows so genders appear alternately in the result set (typically female, male, female, male...). The output must preserve the relative ordering logic while ensuring the alternating pattern.
Approach 1: Window Function with Partitioned Row Numbers (O(n log n) time, O(n) space)
This approach assigns a sequence number to each gender group using the SQL window function ROW_NUMBER(). Partition the rows by gender and order them by user_id. Each gender group now has its own incremental index. The key insight: when you sort the final result by the generated row number first and then by gender, rows from different partitions interleave naturally. For example, the first female and first male both have row_number = 1, so they appear together before the next pair. The database performs a final sort to produce the alternating pattern. This method is concise, deterministic, and widely supported in modern SQL engines such as MySQL 8+.
This technique relies on database querying concepts and especially SQL window functions. The window function isolates ranking logic within each gender group, while the final ORDER BY merges them into a single alternating sequence.
Approach 2: Conditional Ordering with Calculated Position (O(n log n) time, O(n) space)
Another way to interleave rows is by calculating a synthetic ordering position. First compute a per‑gender index using ROW_NUMBER(). Then generate a final ordering value such as 2 * row_number for one gender and 2 * row_number - 1 for the other. Sorting by this calculated value places rows in alternating slots. This approach explicitly encodes the alternating pattern in the ordering formula rather than relying on secondary sorting by gender.
The technique still depends on window functions and sorting, but it gives you precise control over which gender appears first. It also makes the alternating pattern easier to reason about when additional categories are introduced.
Recommended for interviews: The window function partitioning approach is the most common and readable SQL solution. Interviewers expect you to recognize that assigning per‑group row numbers allows the database to interleave rows naturally when sorted. Understanding SQL window functions demonstrates strong practical database skills.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Window Function with Partitioned ROW_NUMBER | O(n log n) | O(n) | Best general solution when the database supports window functions (MySQL 8+, PostgreSQL, SQL Server) |
| Calculated Alternating Position | O(n log n) | O(n) | Useful when you want explicit control over which gender appears first in the ordering |
LeetCode Medium 2308 "Arrange Table by Gender" Interview SQL Question with Detailed Explanation • Everyday Data Science • 2,026 views views
Watch 1 more video solutions →Practice Arrange Table by Gender with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor