Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given two integers n and k, return the kth lexicographically smallest integer in the range [1, n].
Example 1:
Input: n = 13, k = 2 Output: 10 Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.
Example 2:
Input: n = 1, k = 1 Output: 1
Constraints:
1 <= k <= n <= 109Problem summary: Given two integers n and k, return the kth lexicographically smallest integer in the range [1, n].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Trie
13 2
1 1
count-special-integers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #440: K-th Smallest in Lexicographical Order
class Solution {
private int n;
public int findKthNumber(int n, int k) {
this.n = n;
long curr = 1;
--k;
while (k > 0) {
int cnt = count(curr);
if (k >= cnt) {
k -= cnt;
++curr;
} else {
--k;
curr *= 10;
}
}
return (int) curr;
}
public int count(long curr) {
long next = curr + 1;
long cnt = 0;
while (curr <= n) {
cnt += Math.min(n - curr + 1, next - curr);
next *= 10;
curr *= 10;
}
return (int) cnt;
}
}
// Accepted solution for LeetCode #440: K-th Smallest in Lexicographical Order
func findKthNumber(n int, k int) int {
count := func(curr int) int {
next := curr + 1
cnt := 0
for curr <= n {
cnt += min(n-curr+1, next-curr)
next *= 10
curr *= 10
}
return cnt
}
curr := 1
k--
for k > 0 {
cnt := count(curr)
if k >= cnt {
k -= cnt
curr++
} else {
k--
curr *= 10
}
}
return curr
}
# Accepted solution for LeetCode #440: K-th Smallest in Lexicographical Order
class Solution:
def findKthNumber(self, n: int, k: int) -> int:
def count(curr):
next, cnt = curr + 1, 0
while curr <= n:
cnt += min(n - curr + 1, next - curr)
next, curr = next * 10, curr * 10
return cnt
curr = 1
k -= 1
while k:
cnt = count(curr)
if k >= cnt:
k -= cnt
curr += 1
else:
k -= 1
curr *= 10
return curr
// Accepted solution for LeetCode #440: K-th Smallest in Lexicographical Order
impl Solution {
pub fn find_kth_number(n: i32, k: i32) -> i32 {
fn count(mut curr: i64, n: i32) -> i32 {
let mut next = curr + 1;
let mut total = 0;
let n = n as i64;
while curr <= n {
total += std::cmp::min(n - curr + 1, next - curr);
curr *= 10;
next *= 10;
}
total as i32
}
let mut curr = 1;
let mut k = k - 1;
while k > 0 {
let cnt = count(curr as i64, n);
if k >= cnt {
k -= cnt;
curr += 1;
} else {
k -= 1;
curr *= 10;
}
}
curr
}
}
// Accepted solution for LeetCode #440: K-th Smallest in Lexicographical Order
function findKthNumber(n: number, k: number): number {
function count(curr: number): number {
let next = curr + 1;
let cnt = 0;
while (curr <= n) {
cnt += Math.min(n - curr + 1, next - curr);
curr *= 10;
next *= 10;
}
return cnt;
}
let curr = 1;
k--;
while (k > 0) {
const cnt = count(curr);
if (k >= cnt) {
k -= cnt;
curr += 1;
} else {
k -= 1;
curr *= 10;
}
}
return curr;
}
Use this to step through a reusable interview workflow for this problem.
Store all N words in a hash set. Each insert/lookup hashes the entire word of length L, giving O(L) per operation. Prefix queries require checking every stored word against the prefix — O(N × L) per prefix search. Space is O(N × L) for storing all characters.
Each operation (insert, search, prefix) takes O(L) time where L is the word length — one node visited per character. Total space is bounded by the sum of all stored word lengths. Tries win over hash sets when you need prefix matching: O(L) prefix search vs. checking every stored word.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.