Kth Smallest Delivery Fee in a Row and Column Sorted Rate Card

Last updated: November 29, 2026

Quick Overview

A rate card is an n by n matrix where each row and each column is sorted in non-decreasing order (rows are distance bands, columns are weight bands). Return the kth smallest fee overall.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A rate card is an n by n matrix where each row and each column is sorted in non-decreasing order (rows are distance bands, columns are weight bands). Return the kth smallest fee overall.

An Amazon onsite question that tests binary search on the value domain rather than on indices. The shipping rate card framing fits Amazon Logistics. Interviewers compare a heap solution with value binary search and expect you to explain why the binary search result is always an actual matrix entry.

What the Interviewer Expects
  • Offer the heap approach first: O(k log n)
  • Binary search on value between matrix[0][0] and matrix[-1][-1]
  • Count entries <= mid in O(n) with a staircase walk from the bottom-left
  • Explain why the lower-bound value must exist in the matrix
  • State O(n log(range)) time
Key Topics to Cover
binary search on answer
sorted matrix
staircase search
heaps
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
  • How would you handle a non-square n by m matrix?
  • What if fees were floats?
  • When is the heap approach better than value binary search?
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 by n matrix with sorted rows and columns, integer k with 1 <= k <= n^2. Output: the kth smallest value counting duplicates....

High-Level Design

Define count(x) as the number of entries <= x. It is non-decreasing in x. The answer is the smallest x with count(x) >= k. Compute count with a staircase: start at the bottom-left; if the entry is <= ...


Submit Your Answer
Markdown supported