Table: Transactions
+----------------+---------+ | Column Name | Type | +----------------+---------+ | id | int | | transaction_date | date | | amount | int | +----------------+---------+ id is the primary key for this table. Each row contains information about one transaction.
Write an SQL query to find the IDs of the users who made consecutive transactions on consecutive days and at least once have an amount greater than 1000.
Return the result table ordered by user_id in ascending order.
The result format is in the following example.
Transactions table: +------+------------------+--------+ | id | transaction_date | amount | +------+------------------+--------+ | 1 | 2023-12-01 | 950 | | 2 | 2023-12-02 | 1200 | | 3 | 2023-12-03 | 1300 | | 4 | 2023-12-04 | 1400 | | 5 | 2023-12-05 | 1500 | +------+------------------+--------+ Result table: +---------+ | user_id | +---------+ | 1 | +---------+ For the sample data, the user with ID 1 made transactions on consecutive days (2023-12-01, 2023-12-02, 2023-12-03, 2023-12-04, and 2023-12-05), and at least one of the transactions was greater than $1000. So, the user ID 1 is included in the result table.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem is like trying every possible combination of transactions. We check each combination to see if the amounts are increasing and if the transactions are consecutive.
Here's how the algorithm would work step-by-step:
def find_longest_increasing_transactions(transactions):
max_length = 0
for start_index in range(len(transactions)):
current_length = 1
current_amount = transactions[start_index]
current_index = start_index
# Attempt to extend the sequence from the current starting point
while True:
next_index = -1
for subsequent_index in range(current_index + 1, len(transactions)):
# Find the next transaction that is both later and has a larger amount
if transactions[subsequent_index] > current_amount:
next_index = subsequent_index
break
if next_index == -1:
break
# Update state based on the next transaction
current_length += 1
current_amount = transactions[next_index]
current_index = next_index
# Keep track of the longest sequence found so far
if current_length > max_length:
max_length = current_length
# Brute force search completed, so return the maximum length
return max_lengthWe want to find the longest sequence of transactions where each transaction is bigger than the one before it. The trick is to keep track of the best sequence we've seen so far as we go through the transactions, and restart the sequence whenever we find a smaller transaction.
Here's how the algorithm would work step-by-step:
def find_longest_increasing_transactions(transactions):
longest_sequence = []
current_sequence = []
for transaction_amount in transactions:
# Check if the current transaction extends the existing sequence
if not current_sequence or transaction_amount > current_sequence[-1]:
current_sequence.append(transaction_amount)
else:
# Start a new sequence if the current transaction is not greater
current_sequence = [transaction_amount]
# Update the longest sequence found so far
if len(current_sequence) > len(longest_sequence):
longest_sequence = current_sequence[:]
return longest_sequence| Case | How to Handle |
|---|---|
| Empty input array | Return 0, as there are no transactions to form a sequence. |
| Array with a single transaction | Return 1, as a single transaction always forms a sequence of length 1. |
| Array with all transactions having the same amount | Return 1, since no consecutive transactions are strictly increasing. |
| Array with transactions in strictly decreasing order | Return 1, because the longest increasing consecutive subsequence will always be of length 1. |
| Array with very large transaction amounts that could lead to integer overflow if summed or compared incorrectly. | Ensure that the comparison and length calculation do not result in integer overflow by using appropriate data types or range checks. |
| Array containing a mix of positive, negative, and zero transaction amounts | The solution should correctly handle negative and zero amounts as valid transaction values. |
| Array with maximum allowed size by constraints | The solution's space and time complexity should be efficient enough to handle the maximum input size without exceeding memory or time limits. |
| Presence of duplicate transaction amounts within the input array. | The algorithm should correctly identify increasing sequences even when duplicate amounts exist at non-consecutive indices. |