Delay the Resolution of Each Promise - Problem

You are given a table async_functions that contains function execution records and their completion times. Each function has an execution_id, function_name, original_completion_time (in milliseconds), and a delay_ms value that represents additional delay to be added.

Task: Write a SQL query to calculate the delayed completion time for each function by adding the delay_ms to the original_completion_time. Return the results ordered by the delayed completion time to preserve the execution sequence.

The query should return:

  • execution_id
  • function_name
  • original_completion_time
  • delayed_completion_time (calculated as original_completion_time + delay_ms)

Table Schema

async_functions
Column Name Type Description
execution_id PK int Unique identifier for each function execution
function_name varchar Name of the async function
original_completion_time int Original completion time in milliseconds
delay_ms int Additional delay in milliseconds to be added
Primary Key: execution_id
Note: Each row represents an async function execution with its timing details

Input & Output

Example 1 — Basic Promise Delay
Input Table:
execution_id function_name original_completion_time delay_ms
1 fetchData 100 50
2 saveUser 200 100
3 sendEmail 80 40
Output:
execution_id function_name original_completion_time delayed_completion_time
3 sendEmail 80 120
1 fetchData 100 150
2 saveUser 200 300
💡 Note:

Each function gets its delay added to the original completion time. Results are ordered by delayed completion time: sendEmail (80+40=120), fetchData (100+50=150), saveUser (200+100=300).

Example 2 — Zero Delay Case
Input Table:
execution_id function_name original_completion_time delay_ms
1 quickTask 50 0
2 slowTask 300 25
Output:
execution_id function_name original_completion_time delayed_completion_time
1 quickTask 50 50
2 slowTask 300 325
💡 Note:

When delay_ms is 0, the delayed completion time equals the original completion time. quickTask has no additional delay (50+0=50), while slowTask gets 25ms delay (300+25=325).

Constraints

  • 1 ≤ execution_id ≤ 1000
  • 1 ≤ original_completion_time ≤ 10000
  • 0 ≤ delay_ms ≤ 5000
  • function_name is a non-empty string

Visualization

Tap to expand
Promise Delay Resolution OverviewInput FunctionsfetchData: 100ms + 50mssaveUser: 200ms + 100mssendEmail: 80ms + 40msOriginal + DelayADDDELAYOrdered Output1. sendEmail: 120ms2. fetchData: 150ms3. saveUser: 300msSorted by delayed timeEach promise resolves after original_time + delay_msSQL: SELECT *, (original_completion_time + delay_ms)ORDER BY delayed_completion_time
Understanding the Visualization
1
Input
Async functions with original completion times and delays
2
Calculate
Add delay_ms to original completion time
3
Order
Sort by delayed completion time
Key Takeaway
🎯 Key Insight: Simple arithmetic operations in SQL can effectively model timing delays in asynchronous operations
Asked in
Microsoft 28 Netflix 15
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen