Skip to content
tezvyn:
Menu

Set up a client-side button color A/B test

Source: interviewMediumHow cards are made

Summary

practical experiment wiring on the client.

Key points

stable hashing of a persistent ID into buckets, conditional rendering of the variant, exposure plus click event logging.

Watch out for

re-randomizing on each load so users flicker between variants.

What's really being asked

This probes whether you understand deterministic bucketing, exposure logging, and the difference between assignment and rendering. Color is trivial; the consistency machinery is the real subject.

The full answer

Start with a stable identity: a logged-out user gets a persistent device or cookie ID, a logged-in user gets the account ID. Assignment is deterministic: hash the identity concatenated with the experiment key, take the hash modulo a bucket count, and map ranges to control or variant. The same input always yields the same bucket, so the user is sticky across loads and devices that share the account. The button renders conditionally on the resolved variant. Fire an exposure event the moment the variant is actually shown, not at assignment time, and tag every downstream click event with the experiment id and variant so analysis can attribute outcomes.

The mistakes people make

Using Math.random per page load, which re-randomizes and corrupts both groups. Storing the variant only in memory so it resets on refresh. Logging exposure at assignment even when the component never paints, inflating the denominator. Hardcoding a 50 percent split with no flag system, making rollback a redeploy.

What usually comes next

How do you avoid flicker, where control briefly shows before the variant swaps in? How do you handle a user who logs in mid-session and switches from device ID to account ID? How would you ramp from one percent to fifty percent safely?

A concrete example

Experiment key button-color-v1. User device ID abc123. Compute hash of abc123:button-color-v1, modulo 100 gives 73; buckets 0 to 49 are control, 50 to 99 are variant, so this user sees the green button. On render the client posts an exposure event, and the later sign-up click carries variant equals treatment. Every subsequent visit recomputes 73 and stays green.

Interview question

A client-side A/B test shows the same user a different button color on each page refresh. What is the most likely root cause?

  • a.Variant assignment uses a fresh random draw per load instead of a hash of a stable IDCorrect
  • b.The exposure event is fired too late
  • c.The control group is too small
  • d.The button color CSS is cached by the browser
Why?

Sticky assignment requires hashing a persistent identity; a fresh random draw per load re-buckets the user every time. Exposure timing affects analysis denominators, not which variant renders.

Just read this? Test yourself on what you have been reading.

Read the original → support.optimizely.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ab-testing — each one lists the topics its interview covers.

See open roles