View on GitHub

essay

All about my technical essays and practicals

Q:

Design a Poll widget.

Here is one from twitter.

Maybe you can talk about:

data structure
expiration
data refresh policy
cache / bottleneck
something fun like animations .etc
a11y

It is a vague problem, we should narrow down the scope firstly and clarify the requirement and goal.

So, what is the Poll Widget?

The poll widget is a small website component for data collection. The user can create multiple questions and get a link for sharing. People who visit the link will see the options and vote for their choice. After they submit the vote, the polling result will be displayed.

Requirements Gathering and Clarification


Functional Requirement:

  1. the creator can create the polling and get a sharing link (id)
  2. the creator can click the add button and enter text to create question options
  3. people who visit through the share link can vote and see the results
  4. the polling expires after a certain period
  5. how much style customization it supports

Non-functional Requirement:

  1. auto synchronizing data
  2. multi-device supported
  3. internationalization
  4. browser compatibility
  5. accessibility

Capability and Constraints


  1. Once the polling is created, it cannot be modified
  2. The maximum number of options is 10
  3. The maximum text length of each option is 100 characters

High-Level Design


  1. UI when users are creating polling
  2. UI when people haven’t voted yet
  3. UI when people have voted

Data Entities and Relationships

data model

state

relationship

API Design


id: null -> for creating mode / id -> retrieve data from server

token: to identify whether the user has voted

retrieveData(pollID, token) -> PollDTO

onCreatePolling(title, options, title, expiration, token) -> share link

onVote(pollID, optionsID, token) -> boolean

Deep Dive


Necessary Animations

Realtime Result Display

HTTP short-polling

HTTP long-polling

Server-Sent Event

WebSocket

Server-Sent Event wins because we don’t need full-duplex communication and we should support the mobile network with low latency and reliability.

https://aquil.io/articles/a-comparison-between-websockets-server-sent-events-and-polling

Internationalization

https://strapi.io/blog/how-major-frontend-libraries-handle-i18n

Multi-device support​