Skip to content
Raymond Chen edited this page Aug 1, 2024 · 2 revisions

TIP102 Unit 1 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5 mins
  • 🛠️ Topics: Return Statements

U-nderstand

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?
  • The function get_last() should accept a list of items and return the last item in the list. If the list is empty, it should return None.
HAPPY CASE
Input: ["spider man", "batman", "superman", "iron man", "wonder woman", "black adam"]
Output: "black adam"

EDGE CASE
Input: []
Output: None

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Define the function to check if the list is empty and return the last item if it's not.

1. Define the function `get_last(items)`.
2. Check if `items` is not empty:
    a. If not empty, return the last item using negative indexing (`items[-1]`).
    b. If empty, return `None`.

⚠️ Common Mistakes

  • Forgetting to handle the case when the list is empty.

I-mplement

Implement the code to solve the algorithm.

def get_last(items):
    if items:  # Check if the list is not empty
        return items[-1]  # Return the last item using negative indexing
    else:
        return None  # Return None if the list is empty
Clone this wiki locally