Introduction

Welcome to today's lesson on applying data filtering and aggregation in a real-world scenario using a user management system. We'll start by building a foundational structure that can handle basic user operations. Then, we'll expand it by introducing more advanced functionalities that allow filtering and aggregating user data.

Starter Task Methods

In our starter task, we will implement a class that manages basic operations on a collection of user data, specifically handling adding new users, retrieving user profiles, and updating user profiles.

Here are the starter task methods rewritten for PHP:

  • addUser($user_id, $age, $country, $subscribed) - Adds a new user with the specified attributes and returns true if the user was added successfully, or false if a user with the same $user_id already exists.

  • getUser($user_id) - Returns the user's profile if the user exists; otherwise, returns null.

  • updateUser($user_id, $age = null, $country = null, $subscribed = null) - Updates the user's profile based on non-null parameters. Returns true if the user exists and was updated, false otherwise.

To store the user data, we will use associative arrays.

Starter Task Implementation

Here is the implementation of our starter task in PHP:

This implementation covers all our starter methods. Let's move forward and introduce more complex functionalities.

Introducing New Methods for Data Filtering and Aggregation

With our foundational structure in place, it's time to add functionalities for filtering user data and aggregating statistics.

Here are the new methods to implement:

  • filterUsers($min_age = null, $max_age = null, $country = null, $subscribed = null):
    • Returns the list of user IDs that match the specified criteria. Criteria can be null, meaning that the criterion should not be applied during filtering.
  • aggregateStats() - Returns statistics in the form of an associative array:
    • total_users: Total number of users.
    • average_age: Average age of all users (rounded down to the nearest integer).
    • subscribed_ratio: Ratio of subscribed users to total users (rounded to two decimal places).
Step 1: Adding `filterUsers` Method

This method filters users based on the criteria provided. Let's see how it works in PHP:

The filterUsers method filters users based on min_age, max_age, country, and subscribed status. It checks each user's profile against the provided criteria and adds matching users to the filtered_users array, which is then returned.

Step 2: Adding `aggregateStats` Method

This method aggregates statistics from the user profiles. Let's implement it in PHP:

The aggregate_stats method calculates and returns aggregate statistics about the users, including total_users, average_age, and subscribed_ratio.

The Final Solution

Here's the complete UserManager class with all methods, including the new ones for filtering and aggregation:

Lesson Summary

Great job! Today, you've learned how to effectively handle user data by implementing advanced functionalities like filtering and aggregation on top of a basic system. This is a critical skill in real-life software development, where you often need to extend existing systems to meet new requirements.

I encourage you to practice solving similar challenges to solidify your understanding of data filtering and aggregation. Happy coding, and see you in the next lesson!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal