cswork / libraryOpen practice workspace

Coding questions · Apple

Analyze TSV File for User Page Visits and Patterns

This question evaluates skills in parsing and manipulating time-series user-event data, performing aggregations and path-frequency analysis, and understanding vectorized or parallel computations for performance.

visits

+-----------+-----------+------+
| person_id | timestamp | page |
+-----------+-----------+------+
| 1         | 100       | A    |
| 1         | 110       | B    |
| 1         | 150       | C    |
| 2         | 100       | B    |
| 2         | 120       | C    |
+-----------+-----------+------+
Scenario

You receive a TSV file in which each line contains a user’s chronological page-visit history formatted as timestamp,page and separated by “/t”. Business wants insights on usage and performance optimizations.

Question

Parse the file and return the page with the highest total visit count. 2) For every visit, compute the residence time (current timestamp – next timestamp). Return the page with the greatest total residence time across all users. 3) Treat each user’s ordered page sequence as a path. Return the most frequent complete path (e.g., "A→B→C"). 4) #2 can be slow with explicit loops. Rewrite the residence-time computation so it can execute in parallel / vectorized form (e.g., time[i] – time[i-1]) and explain the performance benefit.

Hints

Load into pandas, sort by person_id & timestamp, use groupby + diff/shift, Counter or groupby agg, and vectorized numpy operations for parallelism.

Most Visited Pages

Return the page or pages with the highest total visit count. Output columns: page, visit_count.

Table visits

columntypeconstraints
person_idINTEGERNOT NULL
timestampINTEGERNOT NULL
pageVARCHAR(10)NOT NULL

Sample data:

pageperson_idtimestamp
A1100
B1110
C1150
B2100
C2120

Expected output

Pages B and C are the most visited with 2 visits each.

pagevisit_count
B2
C2

Hints

  1. GROUP BY page and COUNT(*)
  2. Filter to rows matching the maximum count (ties allowed)

Max Residence Time by Page (Non-Window)

Residence time for a row is defined as next_timestamp - current_timestamp within the same person_id. Using a non-window approach (such as a self-join or correlated subquery), compute residence time per row, then sum residence times per page and return the page or pages with the greatest total residence time. Output columns: page, total_residence_time.

Table visits

columntypeconstraints
person_idINTEGERNOT NULL
timestampINTEGERNOT NULL
pageVARCHAR(10)NOT NULL

Sample data:

pageperson_idtimestamp
A1100
B1110
C1150
B2100
C2120

Expected output

Page B has the greatest total residence time: 60 (B: 40 from person 1 and 20 from person 2).

pagetotal_residence_time
B60

Hints

  1. Self-join each visit to the next later visit for the same person
  2. Use MIN(timestamp) with a > condition to find the next event

Most Frequent User Paths

For each user, concatenate visited pages in chronological order using '→' as the separator to form the user's complete path. Count identical paths across users and return the path or paths with the highest frequency. Output columns: path, frequency.

Table visits

columntypeconstraints
person_idINTEGERNOT NULL
timestampINTEGERNOT NULL
pageVARCHAR(10)NOT NULL

Sample data:

pageperson_idtimestamp
A1100
B1110
C1150
B2100
C2120

Expected output

The most frequent complete paths tie: 'A→B→C' and 'B→C' appear once each.

frequencypath
1A→B→C
1B→C

Hints

  1. Build per-user paths with STRING_AGG ordered by timestamp
  2. Count identical paths and filter to the maximum frequency

Residence Time Using Window Functions

Using a window function, compute residence time per row as LEAD(timestamp) - timestamp within each person_id. Sum residence times per page and return the page or pages with the greatest total residence time. Output columns: page, total_residence_time.

Table visits

columntypeconstraints
person_idINTEGERNOT NULL
timestampINTEGERNOT NULL
pageVARCHAR(10)NOT NULL

Sample data:

pageperson_idtimestamp
A1100
B1110
C1150
B2100
C2120

Expected output

Using LEAD to compute per-row durations, page B has the greatest total residence time of 60.

pagetotal_residence_time
B60

Hints

  1. Use LEAD(timestamp) OVER (PARTITION BY person_id ORDER BY timestamp)
  2. Exclude NULL durations (last event per user)