Solve four algorithmic library problems
Company: Meta
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: These tasks evaluate algorithmic problem-solving, data structure design, stateful system validation, and string-processing correctness by combining an optimization selection problem, a mutable location-management ADT, chronological event-log validation, and constrained anagram checking.
Part 1: Maximum Points from Different Categories
Constraints
- 0 <= len(items) <= 100000
- 0 <= k <= 100000
- Each category is an integer in the range [-10^9, 10^9]
- Each points value is an integer in the range [-10000, 10000]
- You must choose exactly k items when possible
Examples
Input: ([[1, 10], [2, 5], [1, 7], [3, 8]], 2)
Expected Output: 18
Explanation: The best item from category 1 has 10 points, and category 3 has 8 points. Choosing them gives 18.
Input: ([[4, -5], [4, -2], [5, -3], [6, 10]], 2)
Expected Output: 8
Explanation: The best per category values are -2, -3, and 10. The top 2 sum to 8.
Input: ([[1, 4], [1, 9]], 2)
Expected Output: -1
Explanation: There is only one distinct category, so choosing 2 distinct categories is impossible.
Input: ([], 0)
Expected Output: 0
Explanation: Choosing exactly 0 items is allowed and has sum 0.
Input: ([[1, 100]], 1)
Expected Output: 100
Explanation: There is exactly one item and k is 1.
Hints
- For each category, only the item with the maximum points can ever be useful.
- After reducing to one best value per category, the task becomes choosing the k largest values.
Part 2: Library Location Management
Constraints
- 0 <= len(operations) <= 100000
- book_id is a non-empty string
- location is a non-empty string for add and move operations
- All operation names are one of 'add', 'move', 'remove', 'get_location'
- Expected average time per operation should be O(1)
Examples
Input: ([['add', 'B1', 'Main:A1:S1:P1'], ['get_location', 'B1'], ['move', 'B1', 'East:A2:S3:P4'], ['get_location', 'B1']],)
Expected Output: ['added', 'Main:A1:S1:P1', 'moved', 'East:A2:S3:P4']
Explanation: B1 is added, found, moved, and then found at the new location.
Input: ([['add', 'B1', 'L1'], ['add', 'B1', 'L2'], ['get_location', 'B1']],)
Expected Output: ['added', 'exists', 'L1']
Explanation: The second add does not overwrite an existing book.
Input: ([['move', 'B9', 'L9'], ['remove', 'B9'], ['get_location', 'B9']],)
Expected Output: ['missing', 'missing', 'NOT_FOUND']
Explanation: Operations on a missing book report missing or NOT_FOUND.
Input: ([['add', 'B2', 'North:A4:S2:P8'], ['remove', 'B2'], ['get_location', 'B2']],)
Expected Output: ['added', 'removed', 'NOT_FOUND']
Explanation: After removal, B2 no longer has a stored location.
Input: ([],)
Expected Output: []
Explanation: No operations produce no results.
Hints
- A hash map from book_id to location gives efficient add, move, remove, and lookup.
- Decide clear behavior for duplicate adds and operations on missing books before coding.
Part 3: Book Checkout Log Validation
Constraints
- 0 <= len(events) <= 100000
- timestamp is a decimal string representing an integer in the range [0, 10^12]
- book_id and member_id are non-empty strings
- action is one of 'checkout', 'return', or 'renew'
- Timestamps are checked independently per book, not globally across all books
Examples
Input: ([['1', 'B1', 'M1', 'checkout'], ['2', 'B1', 'M1', 'renew'], ['3', 'B1', 'M1', 'return'], ['4', 'B2', 'M2', 'checkout']],)
Expected Output: [True, -1]
Explanation: All actions are consistent and timestamps for each book are non-decreasing.
Input: ([['1', 'B1', 'M1', 'checkout'], ['2', 'B1', 'M2', 'checkout']],)
Expected Output: [False, 1]
Explanation: B1 is already checked out by M1 when M2 tries to check it out.
Input: ([['5', 'B7', 'M3', 'return']],)
Expected Output: [False, 0]
Explanation: A return cannot happen before a checkout.
Input: ([['1', 'B1', 'M1', 'checkout'], ['2', 'B1', 'M2', 'renew']],)
Expected Output: [False, 1]
Explanation: Only the current holder M1 can renew B1.
Input: ([['10', 'B1', 'M1', 'checkout'], ['9', 'B1', 'M1', 'renew']],)
Expected Output: [False, 1]
Explanation: The second event for B1 has an earlier timestamp than the previous B1 event.
Hints
- Track the current holder of each checked-out book.
- Track the last timestamp seen for each book before validating the action.
Part 4: Two String Validation Without Counter
Constraints
- 0 <= len(s), len(t) <= 100000
- s and t contain only lowercase English letters 'a' through 'z'
- Do not use collections.Counter or similar built-in frequency-counting utilities
Examples
Input: ('listen', 'silent')
Expected Output: True
Explanation: Both strings contain the same letters with the same frequencies.
Input: ('rat', 'car')
Expected Output: False
Explanation: The character counts differ.
Input: ('', '')
Expected Output: True
Explanation: Two empty strings are anagrams of each other.
Input: ('a', 'aa')
Expected Output: False
Explanation: The strings have different lengths.
Input: ('aabbcc', 'abcabc')
Expected Output: True
Explanation: Both strings have two a's, two b's, and two c's.
Hints
- If the strings have different lengths, they cannot be anagrams.
- A fixed-size array of 26 counts is enough for lowercase English letters.