Arrange Table by Gender - Problem

You're working with a Genders database table that contains user information including their gender identity. This table has an interesting property: it contains exactly equal numbers of users with genders 'female', 'male', and 'other'.

Your mission: Rearrange the table rows to create a specific alternating pattern where the genders cycle through 'female' → 'other' → 'male' repeatedly. Additionally, within each gender group, the user_id values should be sorted in ascending order.

Column NameType
user_idint
gendervarchar

Example: If you have 6 users (2 female, 2 male, 2 other), your result should show: Female(smallest ID) → Other(smallest ID) → Male(smallest ID) → Female(largest ID) → Other(largest ID) → Male(largest ID)

This creates a beautiful, organized pattern that's both gender-balanced and ID-ordered! 🎯

Input & Output

example_1.sql — Basic Example
$ Input: Genders table: +--------+--------+ | user_id| gender | +--------+--------+ | 1 | female | | 2 | other | | 3 | male | | 4 | male | | 5 | other | | 6 | male | | 7 | female | | 8 | other | | 9 | female | +--------+--------+
Output: +--------+--------+ | user_id| gender | +--------+--------+ | 1 | female | | 2 | other | | 3 | male | | 7 | female | | 5 | other | | 4 | male | | 9 | female | | 8 | other | | 6 | male | +--------+--------+
💡 Note: The pattern alternates female → other → male. Within each gender, user_ids are in ascending order: females (1,7,9), others (2,5,8), males (3,4,6). The result shows this perfect alternating pattern.
example_2.sql — Minimum Case
$ Input: Genders table: +--------+--------+ | user_id| gender | +--------+--------+ | 10 | male | | 5 | female | | 15 | other | +--------+--------+
Output: +--------+--------+ | user_id| gender | +--------+--------+ | 5 | female | | 15 | other | | 10 | male | +--------+--------+
💡 Note: With exactly one user per gender, the result simply follows the female → other → male pattern with the single user from each group.
example_3.sql — Large IDs
$ Input: Genders table: +--------+--------+ | user_id| gender | +--------+--------+ | 1000 | other | | 999 | female | | 1001 | male | | 1002 | female | | 1003 | other | | 1004 | male | +--------+--------+
Output: +--------+--------+ | user_id| gender | +--------+--------+ | 999 | female | | 1000 | other | | 1001 | male | | 1002 | female | | 1003 | other | | 1004 | male | +--------+--------+
💡 Note: Even with large user_ids, the algorithm works correctly. Females (999, 1002) and others (1000, 1003) and males (1001, 1004) are each sorted by ID, then alternated in the female → other → male pattern.

Visualization

Tap to expand
🎭 Dance Formation OrganizerArranging dancers in alternating pattern by style and experience🩰BalletExperience: 1,7,9🎤Hip-hopExperience: 2,5,8💃BallroomExperience: 3,4,6Position FormulaBallet: (rank-1)×3+1Hip-hop: (rank-1)×3+2Ballroom: (rank-1)×3+3Final FormationPosition 1🩰 (exp: 1)BalletPosition 2🎤 (exp: 2)Hip-hopPosition 3💃 (exp: 3)BallroomPosition 4🩰 (exp: 7)Ballet...PatternContinues
Understanding the Visualization
1
Group by Dance Style
Separate dancers into their respective groups: Ballet (female), Hip-hop (other), and Ballroom (male)
2
Rank by Experience
Within each dance style, rank dancers by their experience level (user_id) using ROW_NUMBER()
3
Calculate Position Formula
Assign final positions: Ballet gets (rank-1)*3+1, Hip-hop gets (rank-1)*3+2, Ballroom gets (rank-1)*3+3
4
Arrange Formation
Combine all groups and sort by calculated position to create the alternating Ballet → Hip-hop → Ballroom pattern
Key Takeaway
🎯 Key Insight: The ROW_NUMBER() window function combined with calculated positioning formulas creates an elegant single-query solution that perfectly alternates genders while maintaining sorted order within each group.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

ROW_NUMBER() requires sorting within each gender group, and final ORDER BY - overall O(n log n)

n
2n
Linearithmic
Space Complexity
O(n)

Result set storage and temporary space for window function processing

n
2n
Linearithmic Space

Constraints

  • The table contains equal numbers of 'female', 'male', and 'other' genders
  • 1 ≤ user_id ≤ 106
  • 3 ≤ total rows ≤ 104 (and divisible by 3)
  • Gender values are exactly 'female', 'male', 'other'
  • user_id is unique (primary key constraint)
Asked in
Facebook 25 Google 20 Amazon 18 Microsoft 15
28.5K Views
Medium Frequency
~15 min Avg. Time
890 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