-
Notifications
You must be signed in to change notification settings - Fork 254
Get Rank
Sar Champagne Bielert edited this page Apr 19, 2024
·
5 revisions
Unit 5 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- How is the order of players determined in this context?
- Each player has an
ahead
attribute pointing to the player directly ahead of them in the race. This linked structure helps to determine their position.
- Each player has an
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the linked list structure formed by the ahead
attributes of Player objects to determine the position of my_player
in the race.
1) Initialize a counter `place` to 1, as the player is at least in the race.
2) Start with `current` pointing to `my_player`.
3) Traverse the list by following the `ahead` pointers, incrementing `place` until there are no more `ahead` pointers (i.e., until reaching the race leader).
4) Return the `place` number, which indicates the player's position in the race.
- Incorrectly initializing the starting place, potentially off by one error.
- Failing to correctly follow the ahead pointers, which could either loop indefinitely or stop prematurely.
def get_place(my_player):
place = 1 # Start counting from 1 as the player is at least in some place
current = my_player
while current.ahead:
place += 1
current = current.ahead
return place