Problem · Hash Table

Stock System with Updates and Percentage Change

Learn this problem
MediumLead BankONSITE INTERVIEW

Problem statement

Implement a stock-record system. Each initial row in records is [ticker, date, price], where price is a positive integer written as a decimal string. A record is uniquely identified by its ticker and ISO date.

Every initial record has an updated marker set to false. Process each row in operations and append one result string:

  • ["GET", ticker, date]: Return price|updated, such as 12500|true. Reading does not change the marker.
  • ["UPDATE", ticker, date, newPrice]: Replace the price, set updated to true, and return newPrice|true.
  • ["PERCENTAGE_CHANGE", ticker, startDate, endDate]: Compute (endPrice - startPrice) * 100 / startPrice. Truncate toward zero to two decimal places and return exactly two digits after the decimal followed by %.
  • ["BETTER_PERFORMER", firstTicker, secondTicker, startDate, endDate]: Return the ticker with the larger percentage change over those dates. If both changes are equal, return the lexicographically smaller ticker.

If an operation needs any record that does not exist, append NOT_FOUND and do not change state. Percentage comparison uses the exact ratio rather than the displayed two-decimal value. Return all result strings in operation order.

Function

processStockOperations(records: String[][], operations: String[][]) → String[]

Examples

Example 1

records = [["AAPL","2026-01-02","10000"],["AAPL","2026-01-03","12500"],["MSFT","2026-01-02","20000"],["MSFT","2026-01-03","22000"]]operations = [["GET","AAPL","2026-01-02"],["UPDATE","AAPL","2026-01-02","11000"],["GET","AAPL","2026-01-02"],["PERCENTAGE_CHANGE","AAPL","2026-01-02","2026-01-03"],["BETTER_PERFORMER","AAPL","MSFT","2026-01-02","2026-01-03"],["GET","GOOG","2026-01-02"]]return = ["10000|false","11000|true","11000|true","13.63%","AAPL","NOT_FOUND"]

The update changes AAPL's starting price to 11000 and permanently sets that record's marker. Its percentage change is (12500 - 11000) * 100 / 11000, truncated to 13.63%, which is greater than MSFT's 10.00%. The GOOG record is absent.

Example 2

records = [["AAA","2026-02-01","100"],["AAA","2026-02-02","90"],["BBB","2026-02-01","200"],["BBB","2026-02-02","180"]]operations = [["PERCENTAGE_CHANGE","AAA","2026-02-01","2026-02-02"],["BETTER_PERFORMER","BBB","AAA","2026-02-01","2026-02-02"],["UPDATE","CCC","2026-02-01","50"],["GET","BBB","2026-02-01"]]return = ["-10.00%","AAA","NOT_FOUND","200|false"]

Both stocks lose exactly 10.00%, so the comparison returns the lexicographically smaller ticker AAA. Updating a missing record returns NOT_FOUND, and the untouched BBB record still has marker false.

Constraints

  • 1 <= records.length <= 10^4
  • 1 <= operations.length <= 10^4
  • Each initial (ticker, date) key is unique.
  • Every ticker is a non-empty uppercase ASCII string, and every date uses YYYY-MM-DD.
  • Every initial and updated price is an integer in [1, 10^9].
  • Every operation has one of the documented shapes.

More Lead Bank problems

drafts saved locally
public String[] processStockOperations(String[][] records, String[][] operations) {
    // write your code here
}
records[["AAPL","2026-01-02","10000"],["AAPL","2026-01-03","12500"],["MSFT","2026-01-02","20000"],["MSFT","2026-01-03","22000"]]
operations[["GET","AAPL","2026-01-02"],["UPDATE","AAPL","2026-01-02","11000"],["GET","AAPL","2026-01-02"],["PERCENTAGE_CHANGE","AAPL","2026-01-02","2026-01-03"],["BETTER_PERFORMER","AAPL","MSFT","2026-01-02","2026-01-03"],["GET","GOOG","2026-01-02"]]
expected["10000|false", "11000|true", "11000|true", "13.63%", "AAPL", "NOT_FOUND"]
checking account