Implement a pin and board service with ownership rules and board follows

Quick Overview

Implement an in-memory service for users, boards and pins where only a board's owner can save or remove pins, a pin can sit on many boards, and users can follow boards they do not own. It tests modeling many-to-many relationships in both directions, validating each operation before writing, and returning sorted, deterministic results.

Implement a pin and board service with ownership rules and board follows

Company: Pinterest

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a pin and board management service that models the core data relationships of a visual bookmarking platform. Users can create boards to organize content, create pins (each with a title and URL), and save pins to boards they own. The system supports a many-to-many relationship between pins and boards: a single pin can appear on multiple boards, and a board can contain multiple pins. Users can also follow other users' boards to stay updated on new content. The service must enforce ownership rules (only the board owner can add or remove pins), prevent users from following their own boards, and handle duplicate or invalid operations gracefully by returning appropriate boolean results. Implement a `PinBoardService` class with the following methods: - `create_user(user_id: str) -> bool`: Create a new user with the given ID. Returns `True` if created successfully, `False` if the user already exists. - `create_board(board_id: str, user_id: str) -> bool`: Create a new board owned by the specified user. Returns `True` if created, `False` if the board already exists or the user does not exist. - `create_pin(pin_id: str, title: str, url: str) -> bool`: Create a new pin with the given ID, title, and URL. Returns `True` if created, `False` if the pin already exists. - `save_pin_to_board(pin_id: str, board_id: str, user_id: str) -> bool`: Save a pin to a board. Only the board owner (`user_id`) can perform this. Returns `True` if saved, `False` if any ID is invalid, the user is not the owner, or the pin is already on the board. - `remove_pin_from_board(pin_id: str, board_id: str, user_id: str) -> bool`: Remove a pin from a board. Only the board owner can perform this. Returns `True` if removed, `False` otherwise. - `follow_board(user_id: str, board_id: str) -> bool`: The user follows a board. A user cannot follow their own board. Returns `True` if followed, `False` otherwise. - `unfollow_board(user_id: str, board_id: str) -> bool`: The user unfollows a board. Returns `True` if unfollowed, `False` if not currently following or any ID is invalid. - `get_pins_on_board(board_id: str) -> List[str]`: Return a sorted list of pin IDs saved to the specified board. Returns an empty list if the board does not exist. - `get_boards_for_pin(pin_id: str) -> List[str]`: Return a sorted list of board IDs that contain the specified pin. Returns an empty list if the pin does not exist. - `get_followed_boards(user_id: str) -> List[str]`: Return a sorted list of board IDs that the user is currently following. Returns an empty list if the user does not exist. - `get_board_followers(board_id: str) -> List[str]`: Return a sorted list of user IDs that are following the specified board. Returns an empty list if the board does not exist. Example 1 (ownership and the many-to-many link): ```text s = PinBoardService() s.create_user("alice") # True s.create_user("bob") # True s.create_board("travel", "alice") # True s.create_board("food", "alice") # True s.create_pin("p2", "Kyoto streets", "https://example.com/kyoto") # True s.create_pin("p1", "Ramen guide", "https://example.com/ramen") # True s.create_pin("p1", "Another title", "https://example.com/other") # False (pin id taken) s.save_pin_to_board("p2", "travel", "alice") # True s.save_pin_to_board("p1", "travel", "alice") # True s.save_pin_to_board("p1", "food", "alice") # True s.save_pin_to_board("p1", "food", "alice") # False (already on the board) s.save_pin_to_board("p2", "food", "bob") # False (bob does not own food) s.get_pins_on_board("travel") # ["p1", "p2"] s.get_boards_for_pin("p1") # ["food", "travel"] s.remove_pin_from_board("p1", "travel", "alice") # True s.get_boards_for_pin("p1") # ["food"] s.get_pins_on_board("travel") # ["p2"] ``` Pin p1 sits on two boards until alice removes it from travel, and both directions of the link reflect the removal. Example 2 (follows and invalid operations): ```text s = PinBoardService() s.create_user("alice") # True s.create_user("bob") # True s.create_user("alice") # False (user exists) s.create_board("travel", "alice") # True s.create_board("travel", "bob") # False (board id taken) s.create_board("art", "carol") # False (carol does not exist) s.follow_board("alice", "travel") # False (own board) s.follow_board("bob", "travel") # True s.follow_board("bob", "travel") # False (already following) s.get_board_followers("travel") # ["bob"] s.get_followed_boards("bob") # ["travel"] s.unfollow_board("bob", "travel") # True s.unfollow_board("bob", "travel") # False (not following) s.get_followed_boards("bob") # [] s.get_board_followers("nope") # [] (board does not exist) ``` ```hint Both directions of each relationship Four of the queries read a relationship from the opposite side of the call that created it. Decide what each write must record so that every query is a direct lookup, and keep the two sides in step. ``` ```hint Check everything before writing Each mutating method has several reasons to return `False`. Settle the order of the existence, ownership and duplicate checks, and make sure no state changes until all of them pass. ``` ### Constraints & Assumptions - User IDs, board IDs, and pin IDs are unique strings. - A board is owned by exactly one user. - Only the board owner can save or remove pins on that board. - A user cannot follow their own board. - Duplicate saves, removes, follows, and unfollows are no-ops returning `False`. - At most `10^3` total operations per test case. ### Clarifying Questions to Ask - Can any user save any pin to their own board, or do pins have an owner or visibility rules of their own? - Does "sorted" mean plain lexicographic order of the ID strings, or a natural order where "p2" comes before "p10"? - Will users, boards or pins ever be deleted? If so, what should happen to the saves and follows that point at them? ### What a Strong Answer Covers - Records for users, boards and pins keyed by ID, with both sides of the pin-board and user-board follow relationships stored as sets. - Mutating methods that check existence, ownership or self-follow, and duplicates before changing any state, then update both sides of the link together. - Query methods that return sorted copies, and empty lists for unknown IDs. - The time and space cost of each operation. - How the in-memory model would map onto tables or a service as the data grows. ### Follow-up Questions - How would you add `delete_board` and `delete_pin` so that no dangling references remain on the other side of each relationship? - A board owner wants to see the most recently saved pins first, 20 at a time. What changes in the data model? - How would this model map onto a relational schema, and which indexes do the four query methods need? - Two requests save the same pin to the same board at the same moment. How do you make sure only one of them returns `True`?

Overview: Implement an in-memory service for users, boards and pins where only a board's owner can save or remove pins, a pin can sit on many boards, and users can follow boards they do not own. It tests modeling many-to-many relationships in both directions, validating each operation before writing, and returning sorted, deterministic results.

Pinterest logo
Pinterest
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a pin and board management service that models the core data relationships of a visual bookmarking platform. Users can create boards to organize content, create pins (each with a title and URL), and save pins to boards they own.

The system supports a many-to-many relationship between pins and boards: a single pin can appear on multiple boards, and a board can contain multiple pins. Users can also follow other users' boards to stay updated on new content.

The service must enforce ownership rules (only the board owner can add or remove pins), prevent users from following their own boards, and handle duplicate or invalid operations gracefully by returning appropriate boolean results.

Implement a PinBoardService class with the following methods:

  • create_user(user_id: str) -> bool : Create a new user with the given ID. Returns True if created successfully, False if the user already exists.
  • create_board(board_id: str, user_id: str) -> bool : Create a new board owned by the specified user. Returns True if created, False if the board already exists or the user does not exist.
  • create_pin(pin_id: str, title: str, url: str) -> bool : Create a new pin with the given ID, title, and URL. Returns True if created, False if the pin already exists.
  • save_pin_to_board(pin_id: str, board_id: str, user_id: str) -> bool : Save a pin to a board. Only the board owner ( user_id ) can perform this. Returns True if saved, False if any ID is invalid, the user is not the owner, or the pin is already on the board.
  • remove_pin_from_board(pin_id: str, board_id: str, user_id: str) -> bool : Remove a pin from a board. Only the board owner can perform this. Returns True if removed, False otherwise.
  • follow_board(user_id: str, board_id: str) -> bool : The user follows a board. A user cannot follow their own board. Returns True if followed, False otherwise.
  • unfollow_board(user_id: str, board_id: str) -> bool : The user unfollows a board. Returns True if unfollowed, False if not currently following or any ID is invalid.
  • get_pins_on_board(board_id: str) -> List[str] : Return a sorted list of pin IDs saved to the specified board. Returns an empty list if the board does not exist.
  • get_boards_for_pin(pin_id: str) -> List[str] : Return a sorted list of board IDs that contain the specified pin. Returns an empty list if the pin does not exist.
  • get_followed_boards(user_id: str) -> List[str] : Return a sorted list of board IDs that the user is currently following. Returns an empty list if the user does not exist.
  • get_board_followers(board_id: str) -> List[str] : Return a sorted list of user IDs that are following the specified board. Returns an empty list if the board does not exist.

Example 1 (ownership and the many-to-many link):

s = PinBoardService()
s.create_user("alice")                                       # True
s.create_user("bob")                                         # True
s.create_board("travel", "alice")                            # True
s.create_board("food", "alice")                              # True
s.create_pin("p2", "Kyoto streets", "https://example.com/kyoto")   # True
s.create_pin("p1", "Ramen guide", "https://example.com/ramen")     # True
s.create_pin("p1", "Another title", "https://example.com/other")   # False (pin id taken)
s.save_pin_to_board("p2", "travel", "alice")                 # True
s.save_pin_to_board("p1", "travel", "alice")                 # True
s.save_pin_to_board("p1", "food", "alice")                   # True
s.save_pin_to_board("p1", "food", "alice")                   # False (already on the board)
s.save_pin_to_board("p2", "food", "bob")                     # False (bob does not own food)
s.get_pins_on_board("travel")                                # ["p1", "p2"]
s.get_boards_for_pin("p1")                                   # ["food", "travel"]
s.remove_pin_from_board("p1", "travel", "alice")             # True
s.get_boards_for_pin("p1")                                   # ["food"]
s.get_pins_on_board("travel")                                # ["p2"]

Pin p1 sits on two boards until alice removes it from travel, and both directions of the link reflect the removal.

Example 2 (follows and invalid operations):

s = PinBoardService()
s.create_user("alice")                       # True
s.create_user("bob")                         # True
s.create_user("alice")                       # False (user exists)
s.create_board("travel", "alice")            # True
s.create_board("travel", "bob")              # False (board id taken)
s.create_board("art", "carol")               # False (carol does not exist)
s.follow_board("alice", "travel")            # False (own board)
s.follow_board("bob", "travel")              # True
s.follow_board("bob", "travel")              # False (already following)
s.get_board_followers("travel")              # ["bob"]
s.get_followed_boards("bob")                 # ["travel"]
s.unfollow_board("bob", "travel")            # True
s.unfollow_board("bob", "travel")            # False (not following)
s.get_followed_boards("bob")                 # []
s.get_board_followers("nope")                # [] (board does not exist)

Constraints & Assumptions

  • User IDs, board IDs, and pin IDs are unique strings.
  • A board is owned by exactly one user.
  • Only the board owner can save or remove pins on that board.
  • A user cannot follow their own board.
  • Duplicate saves, removes, follows, and unfollows are no-ops returning False .
  • At most 10^3 total operations per test case.

Clarifying Questions to Ask Guidance

  • Can any user save any pin to their own board, or do pins have an owner or visibility rules of their own?
  • Does "sorted" mean plain lexicographic order of the ID strings, or a natural order where "p2" comes before "p10"?
  • Will users, boards or pins ever be deleted? If so, what should happen to the saves and follows that point at them?

What a Strong Answer Covers Guidance

  • Records for users, boards and pins keyed by ID, with both sides of the pin-board and user-board follow relationships stored as sets.
  • Mutating methods that check existence, ownership or self-follow, and duplicates before changing any state, then update both sides of the link together.
  • Query methods that return sorted copies, and empty lists for unknown IDs.
  • The time and space cost of each operation.
  • How the in-memory model would map onto tables or a service as the data grows.

Follow-up Questions Guidance

  • How would you add delete_board and delete_pin so that no dangling references remain on the other side of each relationship?
  • A board owner wants to see the most recently saved pins first, 20 at a time. What changes in the data model?
  • How would this model map onto a relational schema, and which indexes do the four query methods need?
  • Two requests save the same pin to the same board at the same moment. How do you make sure only one of them returns True ?