Magicsheet logo

Convert Date to Binary

Easy
12.5%
Updated 8/1/2025

Asked by 1 Company

Convert Date to Binary

What is this problem about?

The Convert Date to Binary interview question asks you to take a date string in the format "YYYY-MM-DD" and return a string where each part (Year, Month, Day) is converted to its binary representation, separated by dashes.

Why is this asked in interviews?

Google uses the Convert Date to Binary coding problem as a basic string parsing and number formatting test. It evaluates if a candidate can handle string splitting, integer conversion, and custom binary string generation without relying on expensive library calls.

Algorithmic pattern used

This utilizes the Math, String interview pattern.

  1. Split the string using the "-" delimiter.
  2. For each numeric component:
    • Convert the string to an integer.
    • Convert the integer to its binary string representation (usually using bitwise operators or a recursive division-by-2 approach).
  3. Join the three binary strings with dashes.

Example explanation

Input: "2080-02-29"

  1. Year 2080: Binary is 100000100000.
  2. Month 02: Binary is 10.
  3. Day 29: Binary is 11101. Result: "100000100000-10-11101".

Common mistakes candidates make

  • Leading Zeros: Including leading zeros in the binary strings (e.g., 0010 instead of 10).
  • Parsing Errors: Failing to handle the month/day as numbers (treating "02" differently than 2).
  • Delimiter management: Messing up the join operation and ending up with extra or missing dashes.

Interview preparation tip

Be comfortable with manual decimal-to-binary conversion. Even though most languages have bin() or toString(2), interviewers might ask you to implement it manually to see your understanding of the "Divide by 2" algorithm.

Similar Questions