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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
Factorial number system
Combinatorics
Permutation ranking
Math
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported