Your question is Refactor for Memory Efficiency. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Gametime United's inventory service loads ticket availability for a single event at a time, but it also caches results so the next lookup for that event is fast. On a big on-sale night with thousands of concurrent events, the service's memory usage climbs until it gets OOM-killed.
func LoadAvailableTickets(eventID string, allTickets []Ticket) []Ticket {
var available []Ticket
var eventTickets []Ticket
for _, t := range allTickets {
if t.EventID == eventID {
eventTickets = append(eventTickets, t)
}
}
for _, t := range eventTickets {
if t.Status == "AVAILABLE" {
available = append(available, t)
}
}
cache[eventID] = allTickets
return available
}
Can you refactor this function to improve its memory usage? Explain exactly what's driving the growth and what you'd change.