-
Notifications
You must be signed in to change notification settings - Fork 253
Update Rankings
TIP102 Unit 5 Session 1 Advanced (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Linked List, Node Swapping
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What should the function do if the target index is 1?
- Return the original list since there's no previous node to swap with.
- How should the function handle an empty list?
- Return
None
.
- Return
HAPPY CASE
Input: Linked List: mario -> peach -> luigi -> daisy, target = 3
Output: mario -> luigi -> peach -> daisy
Explanation: The nodes at indices 2 and 3 are swapped.
EDGE CASE
Input: Linked List: None, target = 1
Output: None
Explanation: The list is empty.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Linked List problems, we want to consider the following approaches:
- Swapping nodes at given positions.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list to find the nodes at indices target - 1
and target
, and swap them.
1) If the target is 1, return the head as there is no previous node to swap with.
2) Initialize a pointer to traverse the list and keep track of the current index.
3) Traverse the linked list to find the nodes at indices `target - 1` and `target`.
4) Swap the player names of the nodes at `target - 1` and `target`.
5) Return the head of the modified list.
- Forgetting to handle the case where the target is the first node.
- Incorrectly swapping the nodes' data instead of swapping the nodes themselves.
Implement the code to solve the algorithm.
class Node:
def __init__(self, player, next=None):
self.player = player
self.next = next
# For testing
def print_linked_list(head):
current = head
while current:
print(current.player, end="" -> "" if current.next else ""\n"")
current = current.next
def increment_rank(head, target):
if target <= 1 or head is None or head.next is None:
return head
index = 1
prev = None
current = head
while index < target:
prev = current
current = current.next
index += 1
temp = prev.player
prev.player = current.player
current.player = temp
return head
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Trace through your code with an input to check for the expected output.
- Catch possible edge cases and off-by-one errors.
Evaluate the performance of your algorithm and state any strong/weak or future potential work. Assume N represents the number of nodes in the linked list.
- Time Complexity: O(N) because we need to traverse up to the target node.
- Space Complexity: O(1) because we only need a fixed amount of extra space for pointers.