{"id":34235703,"url":"https://github.com/bediger4000/fair-coin-flipping","last_synced_at":"2026-03-10T16:17:50.621Z","repository":{"id":125328699,"uuid":"280503629","full_name":"bediger4000/fair-coin-flipping","owner":"bediger4000","description":"Interview problem asking about rounds of flipping many coins at once","archived":false,"fork":false,"pushed_at":"2021-02-19T20:49:15.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-12-19T08:35:27.888Z","etag":null,"topics":["daily-coding-problem","go","interview-questions"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bediger4000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-17T18:59:56.000Z","updated_at":"2025-09-29T19:04:07.000Z","dependencies_parsed_at":"2023-08-11T03:32:44.186Z","dependency_job_id":null,"html_url":"https://github.com/bediger4000/fair-coin-flipping","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bediger4000/fair-coin-flipping","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bediger4000%2Ffair-coin-flipping","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bediger4000%2Ffair-coin-flipping/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bediger4000%2Ffair-coin-flipping/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bediger4000%2Ffair-coin-flipping/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bediger4000","download_url":"https://codeload.github.com/bediger4000/fair-coin-flipping/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bediger4000%2Ffair-coin-flipping/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30342165,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T15:55:29.454Z","status":"ssl_error","status_checked_at":"2026-03-10T15:54:58.440Z","response_time":106,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["daily-coding-problem","go","interview-questions"],"created_at":"2025-12-16T02:24:18.898Z","updated_at":"2026-03-10T16:17:50.614Z","avatar_url":"https://github.com/bediger4000.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Daily Coding Problem: Problem #598 [Easy]\n\n## Problem\n\nThis problem was asked by Microsoft.\n\nYou have n fair coins and you flip them all at the same time. Any that come up\ntails you set aside. The ones that come up heads you flip again. How many\nrounds do you expect to play before only one coin remains?\n\nWrite a function that, given n, returns the number of rounds you'd expect to\nplay until one coin remains.\n\n## Analysis\n\nThe problem statement emphasises \"fair coins\",\nwhich appears to have a [precise technical meaning](https://en.wikipedia.org/wiki/Fair_coin).\nWhat about unfair coins?\nLooks like it's [surprisingly hard](https://izbicki.me/blog/how-to-create-an-unfair-coin-and-prove-it-with-math.html)\nto actually make a biased coin, at least by bending the coin.\nIf you limit yourself to disk (short cylinder) coins,\nit just might be [impossible](http://www.stat.columbia.edu/~gelman/research/published/diceRev2.pdf).\n\nBased on eliminating half the coins at each mass flip,\nyou'd expect to make log\u003csub\u003e2\u003c/sub\u003e(n) rounds before only one \ncoin remains.\nThis follows the same reasoning that lets us say\nthat binary search makes log\u003csub\u003e2\u003c/sub\u003e(n) comparisons before\nfinding the searched-for item.\n\nYou could also reason that (most times) half the coins turn up heads,\nand thus get eliminated.\nHow many halvings does it take to get to 1?\n\nI went on to write [a program](main.go) that tells you log\u003csub\u003e2\u003c/sub\u003e(n),\nand then does 10,000 iterations of the rounds of flipping n coins\nas described above.\n\n```\n% go build faircoins.go\n% ./faircoins 256\n256 fair coins, log2(256) = 8.000000\n10000 iterations gives 7.879900 average attempts\n```\n\nThe simulation always comes out lower than log\u003csub\u003e2\u003c/sub\u003e(n),\neven if you do many more than 10,000 iterations of rounds of coin flipping.\nI really don't know why.\nThe simulation can \"overshoot\" (more rounds than predicted) as well as \"undershoot\"\n(fewer rounds than predicted), so it's not a systemic problem.\nThe only thing I can think of is that `rand.Intn(2)`\nis very slightly biased.\nSo I wrote [a program ](intntest.go) to test that.\nIt's not biased that I can tell.\n\nAs far as interview questions go,\nit won't test any programming knowledge.\nThe question is phrased in a way that the candidate gets\nnudged towards maybe calculating log\u003csub\u003e2\u003c/sub\u003e of numbers.\nUnless the position is for numerical analyst,\nyou don't want this.\nEven if someone writes the same sort of simulation I did,\nthere's very little programming,\nno room for the candidate to riff on test cases,\ncorner cases or potential optimizations.\nIt doesn't even demand much error handling.\n\nIf you used it as one question of several,\nit might work,\nbut otherwise,\na silly question for an interview.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbediger4000%2Ffair-coin-flipping","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbediger4000%2Ffair-coin-flipping","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbediger4000%2Ffair-coin-flipping/lists"}