Return the Kth Ordering of N Feed Slots Without Enumerating
Last updated: November 29, 2026
Quick Overview
Given n distinct feed slots labeled 1..n, return the kth permutation in lexicographic order (1-indexed) as a string, without generating the earlier permutations.
Meta
November 29, 20260
0
0 solved
Given n distinct feed slots labeled 1..n, return the kth permutation in lexicographic order (1-indexed) as a string, without generating the earlier permutations.
A Meta onsite question about the factorial number system. The interviewer expects you to reject enumeration, decode k digit by digit using factorial block sizes, and get the 0- versus 1-indexing right.
What the Interviewer Expects
- Note that enumeration is O(n!) and infeasible
- Observe that each leading choice covers (n-1)! permutations
- Convert k - 1 into factorial-base digits
- Pick and remove from the remaining list
- Discuss O(n^2) list removal versus O(n log n) with a Fenwick tree
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Given a permutation, return its rank.
- Make the removals O(log n) for n = 10^5.
- How would you handle repeated slot labels?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: n in 1..9 (or larger with big integers) and k in 1..n!. Output: string of the kth lexicographic permutation of 1..n. Example: n=3, k=3 gives "213" (order: 123, 132, 213, 231, 312, 321)....
High-Level Design
With n remaining items, permutations are grouped by their first element into n blocks of size (n-1)!. The 0-indexed rank k - 1 divided by (n-1)! picks the block, so it picks the first element; the r...