Chain turntable: Threshold file
Stuck or curious how this works? Visualize Tree Inorder Traversal ↗
Inputs
head is the first node of a linked list. In examples, [4, 7] means nodes 4 then 7, linked in that order. [] means no nodes. The runner builds the nodes for you.
Your tasks
- Create a list
reversedValues. Reverse the links in the linked list given byhead. Each node has a number in val and a link in next. After the links are reversed, follow the new list from start to end. Record its values in that order. An empty list gives an emptyreversedValues. - Count the numbers in
reversedValuesthat are at leastk(greater than or equal tok). Store the count inatLeastK. - Complete
chainTurntableThresholdFileso it returnsatLeastK. Return it, do not print it.
Example
If head is [9, 2, -3, 8, 8] and k is 2, then reversedValues is [8, 8, -3, 2, 9] and chainTurntableThresholdFile returns 4.
Rules
- If
reversedValuesis empty, the count is 0. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: head is [18, 0, -7, 11, 18] and k is 8.
TEST DATA 2: head is [5, 5, -6, 5] and k is 2.
Examples
Input: head = [18,0,-7,11,18] k = 8 Output: 3
reversedValues is [18, 11, -7, 0, 18]. Here k is 8. The numbers at least 8 are [18, 11, 18]. There are 3, so return 3.
Input: head = [5,5,-6,5] k = 2 Output: 3
reversedValues is [5, -6, 5, 5]. Here k is 2. The numbers at least 2 are [5, 5, 5]. There are 3, so return 3.
Input: head = [] k = 1 Output: 0
reversedValues is []. Here k is 1. The numbers at least 1 are []. There are 0, so return 0.
Input limits
- 0 <= input size <= 64.
- k is a whole number from 1 to 8. Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
- Linked lists do not loop back to an earlier node. Tree examples list nodes by level; null means a missing child. The runner builds the nodes shown in the examples.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n); extra memory, including the helper list, O(n). n is the number of nodes. These labels describe how work and memory grow with the input.
Practice focus
Build reversedValues first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.