FastPrepVersioned Array
Problems/Versioned Array

Versioned Array

TrueFoundry logoTrueFoundry● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interviewer asked you to design a VersionedArray. The constructor takes a length and starts every value at zero. saveVersion saves the current state of the array and returns a version number, starting from one. update takes an index and a value and changes that element. getValue takes an index and a version number and returns the value at that index from the saved version. The sample was update(0, 5), saveVersion() returning 1, update(0, 6), then getValue(0, 1) returning 5.

The report does not say what happens with a bad index or version, or how large the input can be. Here, every index and version is valid and getValue reads only saved versions. You get the calls as a list of operations instead of a class.

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

Design a versioned array. It starts with length elements, all equal to 0. Process the operations in operations in order. For each index i, args[i] holds the arguments of operations[i].

  • "update" with args[i] = [index, value]: set the element at index of the current array to value. This records nothing.
  • "saveVersion" with args[i] = []: save the current state of the whole array and record its version number. Version numbers start from 1 and go up by 1 with each save.
  • "getValue" with args[i] = [index, versionNumber]: record the value that the element at index had in saved version versionNumber.

Updates after a save change only the current array. They never change a version that was already saved.

Return the recorded values of all "saveVersion" and "getValue" operations, in order.

Function

runVersionedArray(length: int, operations: String[], args: int[][]) → int[]

Examples

Example 1

length = 3operations = ["update","saveVersion","update","getValue"]args = [[0,5],[],[0,6],[0,1]]return = [1,5]

The array starts as [0,0,0]. The update makes it [5,0,0], and "saveVersion" saves that state as version 1, so 1 is recorded.

The next update makes the current array [6,0,0], but version 1 still holds 5 at index 0. So "getValue" records 5.

Example 2

length = 2operations = ["saveVersion","update","update","saveVersion","getValue","getValue","getValue"]args = [[],[1,4],[1,7],[],[1,1],[1,2],[0,2]]return = [1,2,0,7,0]

Version 1 is saved right away, so it is [0,0]. Index 1 is then set to 4 and then to 7, and version 2 saves [0,7].

The reads give 0 (index 1 in version 1), 7 (index 1 in version 2), and 0 (index 0 in version 2). The result is the two version numbers followed by these three values.

Example 3

length = 1operations = ["update","saveVersion","saveVersion","update","saveVersion","getValue","getValue","getValue"]args = [[0,3],[],[],[0,9],[],[0,2],[0,3],[0,1]]return = [1,2,3,3,9,3]

Versions 1 and 2 are both saved while the element is 3, because nothing changes between them. Version 3 is saved after the element becomes 9.

So the reads of versions 2, 3, and 1 record 3, 9, and 3.

Constraints

  • 1 <= length <= 10^4
  • 1 <= operations.length <= 10^4
  • args.length == operations.length
  • Each operations[i] is "update", "saveVersion", or "getValue".
  • For "update", args[i] = [index, value] with 0 <= index < length and 0 <= value <= 10^9.
  • For "saveVersion", args[i] is empty.
  • For "getValue", args[i] = [index, versionNumber] with 0 <= index < length, and versionNumber is a version that has already been saved.

More TrueFoundry Phone Screen questions

  • Interview Slot BookingPHONE SCREEN · Seen Jul 2026
  • Verify Words Sorted by a Custom AlphabetPHONE SCREEN · Seen Jul 2026

Similar questions at other companies

  • Future-Effective Employee SalaryRippling · ONSITE INTERVIEWRippling interview questions
  • Insert Delete GetRandom O(1)Charta Health · PHONE SCREENCharta Health interview questions
  • Portfolio Tickers with Constant-Time Random PickCitadel · PHONE SCREENCitadel interview questions
  • Snapshot Map with Sparse Version HistorySnorkel AI · PHONE SCREEN
  • Time-Based Key-Value StoreOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
  • Build a Pivot Table with TotalsSigma Computing · PHONE SCREEN
  • Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
  • Time-Based Key-Value StoreCockroach Labs · PHONE SCREEN · ONSITE INTERVIEW
  • Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
  • Time Map with Sum and AverageConfluent · PHONE SCREENConfluent interview questions
See TrueFoundry hiring insights
Saved
class Solution {
    public int[] runVersionedArray(int length, String[] operations, int[][] args) {
        // Write your solution here.
    }
}
Checking account…