FastPrepSort Paragraphs by Leading Version

Sort Paragraphs by Leading Version

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Each paragraph begins with a version token followed by a space and arbitrary text. A version token contains one or more non-negative decimal components separated by either . or ,; separators may be mixed.

Sort paragraphs by numeric component sequence. Compare components left to right, treating missing trailing components as zero. If normalized versions are equal, preserve original input order.

Return the complete original paragraph strings in sorted order.

Function

sortVersionedParagraphs(paragraphs: String[]) → String[]

Examples

Example 1

paragraphs = ["2.02.11 second","1,11,03 first","2.2.3 middle"]return = ["1,11,03 first","2.2.3 middle","2.02.11 second"]

Numeric versions are [1,11,3], [2,2,3], and [2,2,11].

Example 2

paragraphs = ["1.0 alpha","1 beta","1.0.0 gamma"]return = ["1.0 alpha","1 beta","1.0.0 gamma"]

All normalized versions are equal, so stable input order is preserved.

Example 3

paragraphs = []return = []

No paragraphs remain empty.

Constraints

  • 0 <= paragraphs.length <= 100000.
  • Each paragraph is nonempty and begins with a valid version token and one space.
  • Each version has at most 20 components, each in [0,10^9].

More Google problems

See Google hiring insights
public String[] sortVersionedParagraphs(String[] paragraphs) {
    // Write your code here.
}
paragraphs["2.02.11 second","1,11,03 first","2.2.3 middle"]
expected["1,11,03 first", "2.2.3 middle", "2.02.11 second"]
Checking account…