Le migliori 150 intervisteFacile

Progetta Twitter

Guida dettagliata e implementazione Python per il problema "Progettare Twitter".

Dichiarazione del problema

Facile

Progetta una versione semplificata di Twitter in cui gli utenti possono pubblicare tweet, seguire/smettere di seguire un altro utente e vedere i 10 tweet più recenti nel feed di notizie dell'utente.

Implementa la classe Twitter:

- Twitter() Inizializza il tuo oggetto Twitter.

- postTweet(userId: int, tweetId: int) Compone un nuovo tweet con ID tweetId dall'utente userId.

- getNewsFeed(userId: int) -> List[int] Recupera i 10 ID tweet più recenti nel feed di notizie dell'utente.

- follow(followerId: int, followeeId: int) L'utente con ID followerId ha iniziato a seguire l'utente con ID followeeId.

- unfollow(followerId: int, followeeId: int) L'utente con ID followerId ha iniziato a smettere di seguire l'utente con ID followeeId.

L'input è un elenco di operazioni e argomenti. Implementa una funzione twitter(operations: list, arguments: list) -> list che restituisce un elenco di risultati (None per costruttore/postTweet/follow/unfollow e List[int] per getNewsFeed).

Vincoli
  • 1 <= userId, followerId, followeeId <= 500
  • 0 <= tweetId <= 10^4
  • All the tweets have unique IDs
  • At most 30000 calls will be made in total

Esempi

Example 1
Input
operations = ["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"], arguments = [[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]]
Output
[None, None, [5], None, None, [6, 5], None, [5]]
Explanation

User 1 posts tweet 5. News feed: [5]. User 1 follows 2. User 2 posts tweet 6. News feed: [6, 5]. User 1 unfollows 2. News feed: [5].

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per heap/coda di priorità come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.