Search⌘ K

Feature #7: Process Transactions

Explore how to process stock trading transaction logs by extracting signed integers from each line safely. Understand how to handle leading whitespace, optional signs, and digit parsing while ensuring the result fits within the 32-bit signed integer range. This lesson helps you implement robust parsing logic crucial for coding interview problems involving transaction processing.

Description

In a stock trading company, several transactions are performed every day. All these transactions are recorded in a log file. The lines in the log file may start with a positive or negative integer that will represent the profit or loss on the transaction, followed by textual details about the transaction. There may be leading whitespace in the line.

Our task is to process one line of the log file and return the integer at the beginning of the line, while ignoring the whitespace until the first non-whitespace character is found. After this, we should check for an optional initial + or - sign followed by as many numerical digits as possible. If the first token on the line is not an integer, we will return 0.

Note: Assume that we can only store integers within the 32-bit signed integer range: [−2312^{31}, 2312^{31} − 1]. If the integer is out of this range, either 2312^{31} − 1 or − ...