FastPrepGroup Transactions by Hour
Problems/Group Transactions by Hour

Group Transactions by Hour

Affirm logoAffirm● EasyFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: Affirm asked this in the coding round of a staff-level onsite. The candidate got a batch of transactions and had to put the ones from the last 24 hours into groups, one group for each hour.

The report gives no input format, examples or limits. It does not say where the 24 hours and the hours begin, or how to order the result. Practice choices here: each transaction is an ID and a time in whole seconds, and the current time is passed in. A transaction counts if it is not in the future and is less than 86,400 seconds old, so one at the current time counts and one exactly 24 hours old does not. Hours are counted back from the current time, not by the clock: hour k holds transactions at least k hours old but less than k + 1 hours old. The answer always has 24 groups, newest hour first, and an hour with no transactions is an empty list. Inside a group, IDs go from earliest time to latest, and equal times go by smaller ID. There are 1 to 10,000 transactions with distinct IDs. IDs, times and the current time are whole numbers from 0 to 1,000,000,000, and the answer is a list of 24 integer lists.

The judged task matches the report at about 70%.

A payments service keeps a log of transactions. You are given transactions, where transactions[i] = [id, time] gives a transaction's unique ID and the time it happened, in seconds. You are also given the current time now, in seconds.

Group the transactions from the past 24 hours by hour. The age of a transaction is now - time. Hour k, for 0 <= k <= 23, holds every transaction that is k full hours old, meaning k * 3600 <= age < (k + 1) * 3600. So hours are counted back from now, not by the clock, and hour 0 is the most recent hour.

A transaction with a negative age (it is in the future) or with age >= 86400 (24 hours or older) is in no group.

Return an array of exactly 24 groups, where group k lists the IDs of the transactions in hour k. Inside a group, order the IDs by time, earliest first, and order transactions with the same time by ID, smallest first. A group with no transactions is an empty array.

Function

groupByHour(transactions: int[][], now: int) → int[][]

Examples

Example 1

transactions = [[101,10800],[102,9000],[103,7200],[104,7300],[105,12000],[106,9000]]now = 10800return = [[104,102,106,101],[103],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
  • now - time is 0 for 101, 1800 for 102 and 106, 3500 for 104, and 3600 for 103. Transaction 105 has time = 12000 > now, so it is in the future and in no group.
  • 101, 102, 104 and 106 are less than 3600 seconds old, so they are in hour 0. In time order that is 104 (7300), then 102 and 106 (both 9000, smaller ID first), then 101 (10800).
  • 103 is exactly 3600 seconds old, so it is in hour 1, not hour 0.
  • Hours 2 to 23 are empty.

Example 2

transactions = [[1,13600],[2,13601],[3,82000],[4,82001]]now = 100000return = [[],[],[],[],[4],[3],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[2]]
  • Transaction 1 is 100000 - 13600 = 86400 seconds old, a full 24 hours, so it is in no group.
  • Transaction 2 is 86399 seconds old, so it is in hour 86399 / 3600 = 23 (rounded down).
  • Transaction 3 is 18000 seconds old, exactly 5 hours, so it is in hour 5. Transaction 4 is 17999 seconds old, so it is in hour 4.
  • Every other hour is empty.

Example 3

transactions = [[8,5],[0,0]]now = 0return = [[0],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]

With now = 0, transaction 0 happened right now, so it is in hour 0. Transaction 8 has time = 5 > now, so it is in the future and in no group. The other 23 hours are empty.

Constraints

  • 1 <= transactions.length <= 10^4
  • transactions[i].length == 2
  • Every ID and every time is an integer from 0 to 10^9.
  • All transaction IDs are distinct.
  • 0 <= now <= 10^9

More Affirm problems

  • Propagating Fraud DetectorPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cross-Day User Trust ScoresPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Count Distinct Underwriting PII ValuesPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
All Affirm interview questionsSee Affirm hiring insights
Saved
class Solution {
    public int[][] groupByHour(int[][] transactions, int now) {
        // Write your code here.
    }
}
Checking account…