Your question is Optimizing C++ Search Code. 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).
Route1's session manager needs to check whether a given session ID is currently active by scanning the in-memory list of active sessions. Here is the current implementation, which searches an unsorted list of active session IDs.
#include <vector>
#include <string>
bool isSessionActive(const std::vector<std::string>& activeSessions, const std::string& sessionId) {
bool found = false;
for (int i = 0; i < activeSessions.size(); i++) {
std::string current = activeSessions[i];
if (current == sessionId) {
found = true;
}
}
return found;
}
The team has flagged that this creates an unnecessary temporary variable on every iteration. Explain exactly what is inefficient about this function and how you would change it to be more efficient, including anything beyond just that one temporary variable.