FastPrepParse a Numeric Toll-Log Timestamp

Parse a Numeric Toll-Log Timestamp

OnePay logoOnePay● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

A toll-booth log line has four space-separated fields: timestamp licensePlate booth boothType. The timestamp is the number of seconds since the logging software started.

The interview report describes fixing a timestamp stored as text by using a numeric float. Its supplied LogEntry class already contains the repaired Float.parseFloat constructor and numeric getTimestamp method; the original erroneous code is unavailable.

For this exercise, implement a finite getTimestamp(logLine) wrapper: parse the first field as an IEEE 754 single-precision (binary32) numeric value, then return that same value through a double-precision return type. The other three fields do not affect the timestamp.

For this exercise, assume round-to-nearest with ties to even. The returned value reflects single-precision rounding, rather than preserving the exact decimal text. No repository editing or file I/O is required.

Function

getTimestamp(logLine: String) → double

Examples

Example 1

logLine = "44776.619 KTB918 310E MAINROAD"return = 44776.6171875

The source test parses 44776.619 as a Java float. Its binary32 numeric value is 44776.6171875, which the practice wrapper returns exactly as double.

Example 2

logLine = "52160.132 ABC123 400W ENTRY"return = 52160.1328125

The second source test parses 52160.132 as a Java float, whose value is 52160.1328125.

Example 3

logLine = "0.125 A0 10E EXIT"return = 0.125

One eighth is exactly representable in binary32.

Constraints

  • For this exercise, assume a valid line with exactly four nonempty fields separated by single ASCII spaces, without leading or trailing spaces.
  • The timestamp token is a nonnegative decimal from 0 through 100000, with 1 through 6 integer digits and optionally 1 through 3 fractional digits. Leading zeros are allowed; signs, exponent notation, NaN and infinity are excluded.
  • For this exercise, assume the complete line has at most 100 characters and the other fields are valid toll-log tokens.

More OnePay problems

See OnePay hiring insights
public double getTimestamp(String logLine) {
    // Write your code here.
    return 0.0;
}
logLine"44776.619 KTB918 310E MAINROAD"
expected44776.6171875
Checking account…