Meta VO Interview Question: Compare Users

16 Views
No Comments

Implement the backend side of a user management page.

Given two user lists, one is the current database and the other is a list of possible changes. The first value in a change record is its user ID. If this value is 0, it is a new user to insert into the database. If the value is greater than 0, it is a user ID. Compare all of the user’s attributes with what is in the database. If there is any difference, update the record.

The implementation should return two lists. The first is a list of records to update. The second is a list of records to insert. The provided code will print the lengths of these lists.

Example

List 1 (Current Users in DB)
1 User1
2 User2

List 2 (Users List from UI)
1 User4
0 User5
2 User2

User ID 1 changes the username from User1 to User4. In the second line, the user ID is 0. A new user is created with the username User5. User ID 2 matches what is in the database, so no changes are made.

Return two lists: [1 User1] and [0 User5].

Function Description

Implement the CompareUsers function as described.

CompareUsers has the following parameters:

  • List<User> usersListInDB: the current database
  • List<User> newUsersList: the new transactions

Return:

  • List<User>[]: [updated, inserted]

This problem is a record-diffing task for a user management backend. You need to compare the current database users against incoming changes, using user ID to distinguish updates from inserts. A value of 0 means a new user should be inserted, while a positive ID should be matched against the existing database and compared field by field. Building a map from user ID to user object is the natural approach, since it makes lookups efficient and keeps the solution straightforward.

END
 0