Day of the Year

Easy
#1085Time: O(1). While there is some overhead in parsing and object creation, for a fixed-format input string, the time taken is constant and does not scale with any input size.Space: O(1). A single `LocalDate` object of a fixed size is created.1 company
Patterns
Data structures
Companies

Prompt

Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.

 

Example 1:

Input: date = "2019-01-09"
Output: 9
Explanation: Given date is the 9th day of the year in 2019.

Example 2:

Input: date = "2019-02-10"
Output: 41

 

Constraints:

  • date.length == 10
  • date[4] == date[7] == '-', and all other date[i]'s are digits
  • date represents a calendar date between Jan 1st, 1900 and Dec 31st, 2019.

Approaches

2 approaches with complexity analysis and trade-offs.

This approach leverages the language's standard date and time libraries to simplify the problem. We parse the input string into a date object and then use a built-in method to directly retrieve the day of the year.

Algorithm

  • Use the built-in date parsing function (e.g., LocalDate.parse() in Java) to convert the input string into a date object.
  • Call the corresponding method (e.g., getDayOfYear()) on the date object to get the day number of the year.
  • Return the result.

Walkthrough

Modern programming languages like Java provide powerful libraries for handling dates and times. In Java 8 and later, the java.time package is the standard.

We can use java.time.LocalDate.parse(date) to convert the input string YYYY-MM-DD directly into a LocalDate object. The format matches the ISO-8601 standard, which is the default for LocalDate.parse.

Once we have the LocalDate object, we can simply call the getDayOfYear() method on it. This method handles all the complexities of calendar calculations, including leap years, internally.

import java.time.LocalDate; class Solution {    public int dayOfYear(String date) {        // The input string "YYYY-MM-DD" is in ISO-8601 format,        // which is the default format for LocalDate.parse().        LocalDate localDate = LocalDate.parse(date);                // getDayOfYear() returns the day of the year, from 1 to 365 or 366.        return localDate.getDayOfYear();    }}

Complexity

Time

O(1). While there is some overhead in parsing and object creation, for a fixed-format input string, the time taken is constant and does not scale with any input size.

Space

O(1). A single `LocalDate` object of a fixed size is created.

Trade-offs

Pros

  • Extremely concise and readable code.

  • Highly reliable as it relies on well-tested standard library functions, eliminating risks of manual calculation errors (e.g., leap year logic).

Cons

  • May introduce a slight performance overhead compared to a direct manual calculation, due to the creation of date objects.

  • Might not be permissible in interview settings that aim to test fundamental algorithm implementation skills.

Solutions

class Solution {public  int dayOfYear(String date) {    int y = Integer.parseInt(date.substring(0, 4));    int m = Integer.parseInt(date.substring(5, 7));    int d = Integer.parseInt(date.substring(8));    int v = y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) ? 29 : 28;    int[] days = {31, v, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    int ans = d;    for (int i = 0; i < m - 1; ++i) {      ans += days[i];    }    return ans;  }}

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.