Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
4
Indexable
   const userSessions = {};

    // Process each log entry
    logs.forEach(log => {
        const [userId, timestamp, action] = log.split(' ');
        const time = parseInt(timestamp, 10);

        // Initialize user session data
        if (!userSessions[userId]) {
            userSessions[userId] = [];
        }

        // Record sign-in and sign-out times
        if (action === 'sign-in') {
            userSessions[userId].push({ signIn: time, signOut: Infinity });
        } else if (action === 'sign-out') {
            // Find the last sign-in without a sign-out and set its sign-out time
            for (let i = userSessions[userId].length - 1; i >= 0; i--) {
                if (userSessions[userId][i].signOut === Infinity) {
                    userSessions[userId][i].signOut = time;
                    break;
                }
            }
        }
    });

    // Filter user IDs based on maxSpan
    const validUserIds = Object.keys(userSessions).filter(userId => 
        userSessions[userId].some(session => 
            session.signOut !== Infinity && (session.signOut - session.signIn) <= maxSpan
        )
    );

    // Sort user IDs numerically
    return validUserIds.sort((a, b) => parseInt(a, 10) - parseInt(b, 10));
Editor is loading...
Leave a Comment