Katkat

Solve Three Array Coding Problems

VisaSoftware Engineer · Online Assessment

Last updated

This multi-part question evaluates algorithmic problem-solving skills in array manipulation, optimization (maximizing capped contributions), modular combinatorics (counting divisible-sum triplets), and sequence selection/dynamic decision-making for longest selectable non-decreasing subarrays.

An online assessment contains the following three independent coding problems. For each problem, write a function that returns the requested value. Use 64-bit integers where sums or counts may overflow 32-bit integers.

Problem 1: Maximize the capped contribution after boosting values

You are given two integer arrays a and b of the same length n, and an integer k.

For every index i, the default contribution to the total score is:

min(a[i], b[i])

You may choose at most k distinct indices. For each chosen index i, you double b[i] once, so that the contribution at that index becomes:

min(a[i], 2 * b[i])

Each index can be chosen at most once.

Return the maximum possible total score after choosing up to k indices.

Constraints may be assumed as:

  • 1 <= n <= 200000
  • 0 <= k <= n
  • 0 <= a[i], b[i] <= 10^9

Problem 2: Count triplets with a divisible sum

You are given an integer array nums and a positive integer d.

Return the number of index triplets (i, j, k) such that:

  • 0 <= i < j < k < nums.length
  • (nums[i] + nums[j] + nums[k]) is divisible by d

Constraints may be assumed as:

  • 3 <= nums.length <= 200000
  • 1 <= d <= 200000
  • -10^9 <= nums[i] <= 10^9

Problem 3: Find the longest selectable non-decreasing subarray

You are given two integer arrays A and B of the same length n.

For a contiguous subarray of indices [l, r], you may choose exactly one value at each index i: either A[i] or B[i]. The chosen values must form a non-decreasing sequence:

chosen[l] <= chosen[l + 1] <= ... <= chosen[r]

Return the maximum possible length of such a contiguous subarray.

Constraints may be assumed as:

  • 1 <= n <= 200000
  • -10^9 <= A[i], B[i] <= 10^9