Parse a Numeric Toll-Log Timestamp
Updated
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 timestamp is stored as a numeric float rather than as text: the LogEntry constructor parses it with Float.parseFloat, and getTimestamp is a numeric method.
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) → doubleExamples
Example 1
logLine = "44776.619 KTB918 310E MAINROAD"return = 44776.6171875This test parses 44776.619 as a Java float. Its binary32 numeric value is 44776.6171875, which is returned exactly as a double.
Example 2
logLine = "52160.132 ABC123 400W ENTRY"return = 52160.1328125This test parses 52160.132 as a Java float, whose value is 52160.1328125.
Example 3
logLine = "0.125 A0 10E EXIT"return = 0.125One 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
0through100000, with1through6integer digits and optionally1through3fractional digits. Leading zeros are allowed; signs, exponent notation, NaN and infinity are excluded. - For this exercise, assume the complete line has at most
100characters and the other fields are valid toll-log tokens.
More OnePay Phone Screen questions
Similar questions at other companies
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Minimum Difference Between Two Clock TimesMicron Technology · PHONE SCREEN
- Decimal Digits in OrderZoomInfo · PHONE SCREEN
- Add Two Integers Given as StringsJane Street · PHONE SCREENJane Street interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
- Integer to String Without Built-insHudson River Trading · PHONE SCREENHudson River Trading interview questions