Response Time Stats by URL Pattern
Updated
Problem statement
You are given logs, a list of request log lines from a web service, and patterns, a list of URL patterns. For each pattern, report how many requests matched it and the mean and median of their response times.
Each log line has the form "<method> <path> <time>ms", for example "GET /employees/42 135ms": an HTTP method, the request path, and the response time in whole milliseconds followed by ms.
Each pattern has the form "<method> <template>", for example "GET /employees/{id}". A template is written like a path, but any segment (a part between slashes) may be a placeholder {name}. A log line matches a pattern when all of these hold:
- The methods are the same.
- The path and the template have the same number of segments.
- Every template segment that is not a placeholder equals the path segment in the same position. A placeholder matches any one path segment.
Each log line counts only for the first pattern in patterns that it matches. A line that matches no pattern is ignored.
Return one string per pattern, in the order of patterns, of the form "<pattern> count=<c> mean=<m> median=<d>", where:
cis the number of log lines counted for the pattern.mis the average of their response times, rounded down to an integer.dis the median of their response times: after sorting them, the middle one whencis odd, or the average of the two middle ones, rounded down, whencis even.
A pattern that no log line counts for gives count=0 mean=0 median=0.
Function
summarizeResponseTimes(logs: String[], patterns: String[]) → String[]Examples
Example 1
logs = ["GET /employees/7 120ms","POST /payrolls 300ms","GET /employees/12 80ms","GET /employees/7/paystubs 50ms","GET /employees/3 130ms"]patterns = ["GET /employees/{id}","POST /payrolls"]return = ["GET /employees/{id} count=3 mean=110 median=120","POST /payrolls count=1 mean=300 median=300"]GET /employees/{id} matches the three lines with times 120, 80 and 130. Their mean is 330 / 3 = 110, and sorted they are 80, 120, 130, so the median is 120. GET /employees/7/paystubs has three segments, not two, so it matches no pattern and is ignored. POST /payrolls matches one line, so its count is 1 and its mean and median are both 300.
Example 2
logs = ["GET /employees/me 40ms","GET /employees/5 95ms","GET /employees/me 60ms","GET /employees/8 70ms","GET /employees/5 20ms","GET /employees/9 41ms"]patterns = ["GET /employees/me","GET /employees/{id}"]return = ["GET /employees/me count=2 mean=50 median=50","GET /employees/{id} count=4 mean=56 median=55"]Both patterns match GET /employees/me, but the first one in the list wins, so those two lines (40 and 60) count only for GET /employees/me: mean 50, median (40 + 60) / 2 = 50. The other four times, sorted, are 20, 41, 70, 95. The mean is 226 / 4 = 56.5, rounded down to 56. The count is even, so the median is (41 + 70) / 2 = 55.5, rounded down to 55.
Example 3
logs = ["GET /employees/4 30ms","GET /companies/2/employees 15ms"]patterns = ["PUT /employees/{id}","GET /companies/{companyId}/employees"]return = ["PUT /employees/{id} count=0 mean=0 median=0","GET /companies/{companyId}/employees count=1 mean=15 median=15"]No line uses PUT, so the first pattern gives count=0 mean=0 median=0, even though GET /employees/4 has a path it would match. The second line matches the second pattern, whose placeholder {companyId} takes the segment 2.
Constraints
1 <= logs.length <= 10001 <= patterns.length <= 10- Every method is one of
GET,POST,PUT,PATCHandDELETE. - A path is
/followed by 1 to 6 segments separated by single slashes. Each segment has 1 to 12 characters, each a lowercase English letter or a digit. - A template has the same shape as a path, except that a segment may instead be a placeholder:
{, then 1 to 12 English letters, then}. - Every response time is an integer from
0to100000, written without leading zeros. - Log lines and patterns have exactly one space between fields and no other spaces.
- All strings in
patternsare distinct.
More Gusto Onsite Interview questions
Similar questions at other companies
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Parse and Order Structured LogsCoinbase · ONSITE INTERVIEWCoinbase interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW