Magicsheet logo

Reformat Date

Easy
100%
Updated 6/1/2025

Reformat Date

What is this problem about?

The Reformat Date problem converts a date string from "Day Month Year" format (e.g., "20th Oct 2052") to "YYYY-MM-DD" format. This easy coding problem tests string parsing, month name mapping, and date formatting. The string interview pattern is demonstrated.

Why is this asked in interviews?

Goldman Sachs, TikTok, Oracle, Expedia, and Adobe ask this to verify string manipulation and date conversion skills — practical programming fundamentals for backend and data engineering roles.

Algorithmic pattern used

String split + month map + format. Split the string by spaces into [day_str, month_str, year]. Build a month-name-to-number map. Extract numeric day from day_str (remove ordinal suffix 'st','nd','rd','th'). Format: f"{year}-{month:02d}-{day:02d}".

Example explanation

s="20th Oct 2052". Split: ["20th","Oct","2052"]. Day=20. Month={Oct:10}=10. Year=2052. Output: "2052-10-20".

s="6th Jun 1933". Day=6. Month=6. Output: "1933-06-06" (zero-padded).

Common mistakes candidates make

  • Not zero-padding month and day (need %02d / f"{:02d}").
  • Incorrect suffix stripping (st, nd, rd, th — just remove last 2 chars).
  • Hardcoding month names in wrong order or with typos.
  • Not handling single-digit days (must zero-pad to 2 digits).

Interview preparation tip

Date reformatting tests practical string manipulation: splitting, mapping, and formatting. Build the month map as a dictionary at initialization. The ordinal suffix (st/nd/rd/th) is always 2 characters — strip the last 2 chars from day_str. Always zero-pad day and month with 2 digits. Practice similar "convert date format" problems in different language APIs.

Similar Questions