FastPrepParse and Sort Integer Data

Parse and Sort Integer Data

Legora logoLegora● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Parse the text data into integer values and return those values in nondecreasing numeric order.

For this exercise, assume the input is a comma-separated list of signed decimal integers. Each integer may have an optional leading + or -, and surrounding ASCII spaces, tabs, carriage returns, or newlines. Preserve every occurrence, including duplicates. An empty or whitespace-only input represents an empty list.

All nonempty inputs are valid: each comma separates two nonempty integer fields. Numeric order differs from lexicographic text order.

Function

parseAndSort(data: String) → int[]

Examples

Example 1

data = "10, 2, -3, 2"return = [-3,2,2,10]

Numeric sorting places 10 after 2 and preserves both occurrences of 2.

Example 2

data = "  "return = []

Whitespace-only text represents an empty list.

Constraints

  • 0 <= data.length <= 30000.
  • The parsed list contains at most 2000 integers.
  • Every parsed integer lies between -10^9 and 10^9, inclusive.
  • Every nonempty field is a valid signed base-ten integer; no internal whitespace occurs within its sign or digits.
  • Each integer field has at most 10 decimal digits, excluding its optional sign and surrounding whitespace.
See Legora hiring insights
public int[] parseAndSort(String data) {
    // Write your code here.
}
data"10, 2, -3, 2"
expected[-3,2,2,10]
Checking account…