Relational Learning Queries
Updated
Problem statement
For this exercise, assume a small enrollment database has student IDs, course IDs and a relation between them. Each row of enrollments is [student, course]. A student can join several courses, and a course can have several students.
Use this operation guide to read the relations:
select(rows, predicate)keeps the rows satisfying a condition, in their original order.project(rows, column)returns that column from each selected row, in order.related(key, rows, column)selects the relation rows whose chosen column equals the key.count(rows)returns the number of selected rows.
For example, if enrollment rows are [[1,20],[1,10],[2,20]], selecting student 1 and projecting the course column gives [20,10]. Sorting that query result gives [10,20].
Implement enrollmentQueries and return exactly three integer rows:
- All course IDs related to
studentId, in increasing order. - All student IDs related to
courseId, in increasing order. - The number of enrolled students for every course, aligned with the original order of
courseIds. Include zero for a course with no enrollments.
The first two rows are empty when they have no matches. The relation contains no repeated student-course pair. Use ordinary loops or your language's query tools; no external helper library, SQL string renderer or ORM is required.
Function
enrollmentQueries(studentIds: int[], courseIds: int[], enrollments: int[][], studentId: int, courseId: int) → int[][]Examples
Example 1
studentIds = [1,2]courseIds = [20,10,30]enrollments = [[1,20],[1,10],[2,20]]studentId = 1courseId = 20return = [[10,20],[1,2],[2,1,0]]Student 1 joins courses 20 and 10, so the first row is [10,20]. Course 20 has students 1 and 2. Course counts follow [20,10,30], giving [2,1,0].
Example 2
studentIds = [7]courseIds = [40,10]enrollments = []studentId = 7courseId = 10return = [[],[],[0,0]]No enrollment exists. Both selected relations are empty, and both course counts are zero. The result is [[],[],[0,0]].
Example 3
studentIds = [10,2,1000000]courseIds = [50,10,1]enrollments = [[1000000,1],[2,10],[10,1],[2,50]]studentId = 2courseId = 1return = [[10,50],[10,1000000],[1,1,2]]Student 2 joins courses 10 and 50. Course 1 has students 10 and 1000000. Counts for courses [50,10,1] are [1,1,2]; this row keeps the input course order.
Constraints
1 <= studentIds.length <= 50and1 <= courseIds.length <= 50.0 <= enrollments.length <= 200.- Every enrollment row contains exactly
2integers. - Every ID, including both query IDs and every enrollment entry, is in
[1, 1000000]. - Student IDs are distinct, course IDs are distinct, and enrollment pairs are distinct.
- Every enrollment student belongs to
studentIds, and every enrollment course belongs tocourseIds. studentIdbelongs tostudentIds, andcourseIdbelongs tocourseIds.
More Palantir interview questions
- Map and Accumulate Learning QueriesONSITE INTERVIEW · Seen Oct 2026
- Find Unmet Users With Shared InterestsONSITE INTERVIEW · Seen Mar 2025
- Maximum Profit with Repeated Stock TradesONSITE INTERVIEW · Seen Sep 2024
- Minimum Calls for Repeated API LookupsONSITE INTERVIEW · Seen Oct 2022
- Find Cookable RecipesONSITE INTERVIEW · Seen Nov 2019
- Simulate Multiplayer WarONSITE INTERVIEW · Seen Nov 2019
- Trace Terminal Company OwnershipONSITE INTERVIEW · Seen Nov 2019
- Query Words Within K PositionsPHONE SCREEN · Seen Nov 2026
- Basic Item DiscountsOA · Seen Nov 2026
- Optimize Discount Selection Under a Point BudgetOA · Seen Nov 2026
Similar questions at other companies
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Restaurant Vote LeadersZipRecruiter · PHONE SCREEN · ONSITE INTERVIEWZipRecruiter interview questions
- High-Severity Counts by Search TypeChainalysis · ONSITE INTERVIEWChainalysis interview questions
- Intersection of Two ListsJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Pair Left and Right Socks by IDRoblox · ONSITE INTERVIEWRoblox interview questions
- Net Position Calculation from API SnapshotsHeadlands Technologies · ONSITE INTERVIEWHeadlands Technologies interview questions
- Count Word FrequenciesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions