FastPrepSort Tuples by Value and Then Key

Sort Tuples by Value and Then Key

Microsoft logoMicrosoft● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row of pairs is an integer tuple [key, value]. Return all rows ordered by value ascending, then by key ascending when values are equal.

Keep every occurrence of an identical tuple. An empty input returns an empty matrix. Numeric comparisons use signed integer order.

Function

sortValueKeyPairs(pairs: int[][]) → int[][]

Examples

Example 1

pairs = [[4,2],[1,3],[2,2],[1,2]]return = [[1,2],[2,2],[4,2],[1,3]]

Value 2 rows precede value 3; tied values order keys 1, 2, then 4.

Example 2

pairs = []return = []

An empty collection remains empty.

Example 3

pairs = [[2,-1],[-3,-1],[2,-1]]return = [[-3,-1],[2,-1],[2,-1]]

Equal values use numeric key order; repeated identical rows are retained.

Constraints

  • 0 <= pairs.length <= 1000.
  • Every row has exactly two integers, [key, value].
  • Both fields lie in [-2000000000,2000000000].

More Microsoft problems

See Microsoft hiring insights
public int[][] sortValueKeyPairs(int[][] pairs) {
    return new int[0][];
}
pairs[[4,2],[1,3],[2,2],[1,2]]
expected[[1,2],[2,2],[4,2],[1,3]]
Checking account…