Audit logging in GCP - Time & Space Complexity
Audit logging records actions in a system to keep track of what happened and when.
We want to understand how the time to write audit logs changes as more events happen.
Analyze the time complexity of the following audit logging code snippet.
// Pseudocode for audit logging in GCP
function writeAuditLog(events) {
for (let event of events) {
// Prepare log entry
let logEntry = formatLog(event);
// Write log entry to Cloud Logging
cloudLogging.write(logEntry);
}
}
This code writes each event's audit log entry one by one to Cloud Logging.
- Primary operation: Loop over each event to write its log entry.
- How many times: Once for each event in the input list.
As the number of events grows, the number of log writes grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 log writes |
| 100 | 100 log writes |
| 1000 | 1000 log writes |
Pattern observation: The work grows directly with the number of events.
Time Complexity: O(n)
This means the time to write audit logs grows linearly with the number of events.
[X] Wrong: "Writing multiple audit logs at once takes the same time as writing one."
[OK] Correct: Each event requires its own log write, so more events mean more time.
Understanding how audit logging scales helps you design systems that handle many events efficiently.
"What if we batch multiple events into a single log write? How would the time complexity change?"