You're given an array of equal-length numeric strings and need to answer multiple queries efficiently. Each query asks you to:

  1. Trim all numbers to their rightmost trim digits
  2. Find the k-th smallest trimmed number
  3. Return the original index of that number

๐Ÿ” Key Challenge: When two trimmed numbers are equal, the one with the smaller original index is considered smaller.

Example: Given ["102", "473", "251", "814"] and query [2, 1] (2nd smallest, trim to 1 digit):

  • Trimmed: ["2", "3", "1", "4"]
  • Sorted by value then index: ["1"(idx 2), "2"(idx 0), "3"(idx 1), "4"(idx 3)]
  • 2nd smallest โ†’ return index 0

Input & Output

example_1.py โ€” Basic Query Processing
$ Input: nums = ["102", "473", "251", "814"] queries = [[1,1],[2,3],[4,2],[1,2]]
โ€บ Output: [2, 2, 3, 0]
๐Ÿ’ก Note: Query 1: trim=1 โ†’ ["2","3","1","4"] โ†’ sorted: ["1"(idx2), "2"(idx0), "3"(idx1), "4"(idx3)] โ†’ 1st smallest is index 2. Query 2: trim=3 โ†’ ["102","473","251","814"] โ†’ sorted: ["102"(idx0), "251"(idx2), "473"(idx1), "814"(idx3)] โ†’ 2nd smallest is index 2.
example_2.py โ€” Equal Values Test
$ Input: nums = ["24", "37", "96", "04"] queries = [[2,1],[1,2]]
โ€บ Output: [3, 3]
๐Ÿ’ก Note: Query 1: trim=1 โ†’ ["4","7","6","4"] โ†’ sorted: ["4"(idx0), "4"(idx3), "6"(idx2), "7"(idx1)] โ†’ 2nd smallest is index 3 (tie-breaker by index). Query 2: trim=2 โ†’ ["24","37","96","04"] โ†’ sorted: ["04"(idx3), "24"(idx0), "37"(idx1), "96"(idx2)] โ†’ 1st smallest is index 3.
example_3.py โ€” Single Character
$ Input: nums = ["1", "2", "3", "4"] queries = [[3,1],[2,1]]
โ€บ Output: [2, 1]
๐Ÿ’ก Note: All numbers are single characters. After trim=1: ["1","2","3","4"] โ†’ already sorted by index. Query 1: 3rd smallest is "3" at index 2. Query 2: 2nd smallest is "2" at index 1.

Visualization

Tap to expand
๐Ÿ“š Library Catalog Sorting SystemBooks: ["102", "473", "251", "814"]Book ACatalog: 102Book BCatalog: 473Book CCatalog: 251Book DCatalog: 814๐Ÿ‘ค Patron Request: "2nd smallest book by last 1 digit"Last digits: ["2", "3", "1", "4"]"2""3""1""4"๐Ÿ—ƒ๏ธ Sorted & Cached Result for "trim=1":"1" โ†’ Book C(index 2)"2" โ†’ Book A(index 0)"3" โ†’ Book B(index 1)"4" โ†’ Book D(index 3)2nd smallest โ†’ Return Book A (index 0)๐Ÿ”„ Future requests for "trim=1" use cached result instantly!No need to sort again โ†’ O(1) lookup instead of O(n log n) sorting
Understanding the Visualization
1
Patron Request
Patron wants 2nd smallest book looking at last 1 digit of catalog
2
Trim Catalogs
Extract last digit from each catalog number
3
Sort & Cache
Sort books by trimmed numbers, cache the result
4
Future Requests
When another patron asks for same trim pattern, use cached result
Key Takeaway
๐ŸŽฏ Key Insight: Cache sorted results for each trim length. Multiple queries with the same trim can reuse cached data, dramatically improving performance from O(q ร— n log n) to O(unique_trims ร— n log n + q).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(unique_trims ร— n log n + q)

Sort once per unique trim length, then O(1) lookup per query

n
2n
โšก Linearithmic
Space Complexity
O(unique_trims ร— n)

Store sorted results for each unique trim length

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i].length โ‰ค 100
  • nums[i] consists of only digits
  • All strings in nums have equal length
  • 1 โ‰ค queries.length โ‰ค 100
  • queries[i] = [ki, trimi]
  • 1 โ‰ค ki โ‰ค nums.length
  • 1 โ‰ค trimi โ‰ค nums[i].length
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
38.0K Views
Medium Frequency
~25 min Avg. Time
1.5K 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