FastPrepThe Oracles
Problems/The Oracles

The Oracles

Rubrik logoRubrik● MediumINTERNOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

While on his crazy adventures, Rick and Morty stumbled upon an alien species called the Oracles, known for their great mathematical prowess. Among the Oracles was a game called the Oracle, which they taught their infants and toddlers. In this game, there is one dealer and one player (the infant). The dealer deals the player an initially empty array A. The dealer then has the ability to perform two kinds of operations on the list:

  • Append any number x to the end of the array.
  • Copy the array x times back-to-back to A.

The dealer performs an operation but does not reveal the array's content to the player. The Oracle then rapidly asks the player what numbers are present at different indices of the array. Rick thinks the problem is in P, but is it? Help him solve this problem.

Input Format

  • The first line contains two integers n and q, where n denotes the number of operations the dealer performs, and q denotes the number of queries the dealer asks the player.
  • The next n lines each contain two space-separated integers. The i-th line (1 <= i <= n) contains two integers b_i and x_i:

    👉 If b_i is 1, it denotes the operation of the first kind: the array becomes A <- A + [x_i].

    👉 If b_i is 2, it denotes the operation of the second kind: the array becomes A <- [A | A | ... | A] (copied x_i times).

  • - The following line contains q space-separated integers, q_j (1 <= j <= q), where q_j is an index of the array.

Output Format

Print q integers, each representing the value at the queried index of the array.

Function

oracleGame(n: int, q: int, operations: int[][], queries: long[]) → long[]

Examples

Example 1

n = 5q = 10operations = [[1, 5], [1, 2], [2, 1], [1, 3], [2, 1]]queries = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]return = [5, 2, 5, 2, 3, 5, 2, 5, 2, 3]
  • Before the first operation: A = []
  • After the first operation: A = [5]
  • After the second operation: A = [5, 5]
  • After the third operation: A = [5, 5, 2, 2]
  • After the fourth operation: A = [5, 5, 2, 2, 5, 5, 2, 2]
  • After the fifth operation: A = [5, 5, 2, 2, 5, 3, 5, 2, 5, 2, 3]

Constraints

  • - 1 <= n <= 10^5
  • - 1 <= q <= 10^5
  • - b_i belongs to {1, 2}, for all i (1 <= i <= n)
  • - 1 <= x_i <= n for b_i = 1
  • - 1 <= x_i <= 10^9 if b_i = 2
  • - 1 <= q_j <= min(10^18, e) for all j (1 <= j <= q), where e is the final size of the array.

More Rubrik interview questions

  • Colored Rooted TreeONSITE INTERVIEW · Seen Nov 2026
  • Delete and Freeze GameOA · Seen Nov 2026
  • Delete K Subsets for Maximum ScoreOA · Seen Nov 2026
  • Distribute Elements Into Two Arrays IIOA · Seen Nov 2026
  • Minimum Replacements to Sort the ArrayOA · Seen Nov 2026
  • All Balanced Strings from Minimum Parenthesis InsertionsONSITE INTERVIEW · Seen Nov 2026
  • Insert into a Left-Complete Binary TreeONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Request Counts by AttributeONSITE INTERVIEW · Seen Oct 2026
  • Minimum Swaps To Binary PalindromeOA · Seen Apr 2026
  • Get Minimum OperationsOA · Seen Nov 2024

Similar questions at other companies

  • Bank Accounts with Balance HistoryMeta · OAMeta interview questions
  • Index of the Maximum After Left RotationsChubb · OAChubb interview questions
  • Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
  • Forward Block Placement QueriesCapital One · OACapital One interview questions
  • Souvenir Shop PurchasesAmazon · OA
  • Online Meeting Scheduler with Fixed RoomsUber · ONSITE INTERVIEWUber interview questions
  • Detect a Silent SensorConfluent · ONSITE INTERVIEWConfluent interview questions
  • Sorted Insertion SnapshotsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
  • Finish Ordered CPU Tasks with the Fewest CPUsGoogle · PHONE SCREENGoogle interview questions
  • Minimum Non-Adjacent Selection CapabilityMicrosoft · OAMicrosoft interview questions
All Rubrik interview questionsSee Rubrik hiring insights
Saved
class Solution {
    public long[] oracleGame(int n, int q, int[][] operations, long[] queries) {
      // write your code here
    }

}
Checking account…