Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
2
Indexable
Never
   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));
Leave a Comment