FastPrepCalculate the Max Value
Problems/Calculate the Max Value

Calculate the Max Value

Amazon logoAmazon● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Amazon’s engineering team is developing a tool designed to minimize the size of an n x n grid named matrix, based on a compression ratio described by the array limit. Their challenge is to compute the highest possible sum of exactly x elements from the matrix while adhering to the following conditions:

  • For every row i (where 0 ≤ i < n), the count of elements picked from that row must not surpass limit[i].
  • If it turns out impossible to pick exactly x elements, the function should return -1.

Your task is to assist by computing the largest possible sum achievable under these restrictions.

Function

findMaxValue(limit: int[], matrix: int[][], x: int) → long

Complete the function findMaxValue in the editor.

findMaxValue has the following parameters:

  1. int[] limit: an array where each entry is the maximum count of elements allowed to be chosen from the matching row of matrix
  2. int[][] matrix: a two-dimensional square grid of integers
  3. int x: the exact total number of elements to pick

Returns long: the maximum sum when choosing exactly x elements within the limits, or -1 if that is not possible

Examples

Example 1

limit = [1, 2, 1]matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]x = 2return = 15
matrix
012012314562789
The top selections from each row, respecting the limit constraints, are as follows: Row 0: Choose 3 (since limit[0] = 1 allows only one pick) Row 1: Pick 5 and 6 (as limit[1] = 2 permits two selections) Row 2: Choose 9 (since limit[2] = 1 restricts to a single selection) However, only x = 2 items can be chosen in total. The most optimal selection strategy is to pick 6 from Row 1 and 9 from Row 2, maximizing the sum.

Constraints

  • 1 ≤ n ≤ 50
  • 1 ≤ limit[i] ≤ n
  • 1 ≤ matrix[i][j] ≤ 10^4
  • 1 ≤ x ≤ n * n

More Amazon interview questions

  • Lexicographically Largest IdentifierOA · Seen Nov 2026
  • Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
  • Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
  • Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
  • Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
  • Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Fill One Exact-Length Binary WindowOA · Seen Nov 2026

Similar questions at other companies

  • Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
  • Grid Paths with Override PassesIMC Trading · OAIMC Trading interview questions
  • Largest Square of OnesOpenAI · OAOpenAI interview questions
  • Grid Paths with Override PassesMicrosoft · OAMicrosoft interview questions
  • Minimum Moves to Spread Stones Across a GridAmerican Express · OAAmerican Express interview questions
  • Maximum Even-Sum Neighboring PairsDRW · OADRW interview questions
  • Minimize Total Input Cost (for LTMS)Salesforce · OASalesforce interview questions
  • Minimum Operations To Reduce To ZeroUber · OAUber interview questions
  • Maximum Reward (Intuit India)Intuit · OAIntuit interview questions
  • Get Minimum OperationsJP Morgan Chase · OAJP Morgan Chase interview questions

Interview reports that mention this question

  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
See Amazon hiring insights
Saved
class Solution {
    public long findMaxValue(int[] limit, int[][] matrix, int x) {
      // write your code here
    }

}
Checking account…