FastPrepDistribute Elements Into Two Arrays II
Problems/Distribute Elements Into Two Arrays II

Distribute Elements Into Two Arrays II

Rubrik logoRubrik● HardINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was the first question of a 75-minute Rubrik online assessment for a CPD intern role. The post links the public problem Distribute Elements Into Two Arrays II and shows a Python answer that uses a sorted list.

The post gives no limits or examples. This version keeps the usual rules and uses at most 2000 numbers, each from 1 to 10^9, so the full answer list stays small enough to check. The linked problem allows up to 10^5 numbers. With at most 2000 here, simply counting through both arrays for every number also passes. A Fenwick tree or segment tree is what you need at the larger size. The writer noted that the test platform has no sorted list, so they wrote a brute force that passed only a few tests.

Our version is about 75% the same as the reported question.

You are given an integer array nums of length n. Split its elements into two arrays, arr1 and arr2, by going through nums from left to right:

  • nums[0] goes into arr1.
  • nums[1] goes into arr2.
  • For each later element nums[i], count how many elements already in arr1 are strictly greater than nums[i], and count the same for arr2. Append nums[i] to the array with the larger count.
  • If the two counts are equal, append nums[i] to the array that has fewer elements.
  • If the sizes are also equal, append nums[i] to arr1.

Return the array made of all elements of arr1 followed by all elements of arr2.

Function

resultArray(nums: int[]) → int[]

Examples

Example 1

nums = [4,2,5,1]return = [4,5,1,2]

Start with arr1 = [4] and arr2 = [2].

  • For 5: no element of either array is greater, so the counts are 0 and 0. Both arrays have one element, so 5 goes to arr1 = [4, 5].
  • For 1: arr1 has two greater elements (4 and 5) and arr2 has one (2). The larger count wins, so arr1 = [4, 5, 1].

The answer is [4, 5, 1] followed by [2].

Example 2

nums = [5,9,3,9,1]return = [5,3,1,9,9]

Start with arr1 = [5] and arr2 = [9].

  • For 3: each array has one greater element, and both have one element, so arr1 = [5, 3].
  • For 9: no element is greater than 9 in either array. arr2 is shorter, so arr2 = [9, 9].
  • For 1: each array has two greater elements, and each has two elements, so arr1 = [5, 3, 1].

The answer is [5, 3, 1] followed by [9, 9].

Constraints

  • 3 <= n <= 2000
  • 1 <= nums[i] <= 10^9

More Rubrik Online Assessment questions

  • Delete and Freeze GameOA · Seen Nov 2026
  • Delete K Subsets for Maximum ScoreOA · Seen Nov 2026
  • Minimum Replacements to Sort the ArrayOA · Seen Nov 2026
  • Minimum Swaps To Binary PalindromeOA · Seen Apr 2026
  • Get Minimum OperationsOA · Seen Nov 2024
  • Horizontal Pod AutoscalerOA · Seen Nov 2024
  • Stock PricesOA · Seen Nov 2024
  • StarlightOA · Seen Oct 2024
  • Energy CrisisOA · Seen Sep 2024
  • Mike and GemsOA · Seen Sep 2024

Similar questions at other companies

  • Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
  • Subtree Access OverridesPinterest · PHONE SCREENPinterest interview questions
  • Maximize Shipment Order by Sorting One WindowAmazon · OA
  • Incident MonitorStripe · OAStripe interview questions
  • Count Pairs with K Divisible Values in the IntervalChubb · OAChubb interview questions
  • Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
  • Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions
  • Debug the Impact ModelMillennium · OAMillennium interview questions
  • Four-Player Trick-Taking Card Game SimulationScale AI · OA · PHONE SCREENScale AI interview questions
  • Binary Sorting Rounds After FlipsVisa · OAVisa interview questions
All Rubrik interview questionsSee Rubrik hiring insights
Saved
class Solution {
    public int[] resultArray(int[] nums) {
        // Write your code here.
    }
}
Checking account…