BackmediumArraysarraysmedium

Longest Holiday Free Subsequence Solution

Problem Statement

Given an array of strings representing days of the week and an array of strings representing holidays, determine the length of the longest subsequence that does not contain any holidays. A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

Example 1
Input
["Monday", "Tuesday", "Wednesday"], ["Tuesday"]
Output
2

Explanation: Step-by-step: with input ["Monday", "Tuesday", "Wednesday"] and holidays ["Tuesday"], we first identify the holidays in the given array. Then, we find the longest subsequence that does not contain any holidays. In this case, the longest subsequence is ["Monday", "Wednesday"], giving output 2.

Example 2
Input
["Monday", "Tuesday", "Wednesday", "Thursday"], ["Monday", "Wednesday"]
Output
1

Explanation: Step-by-step: with input ["Monday", "Tuesday", "Wednesday", "Thursday"] and holidays ["Monday", "Wednesday"], we first identify the holidays in the given array. Then, we find the longest subsequence that does not contain any holidays. In this case, the longest subsequence is ["Tuesday"] or ["Thursday"], giving output 1.

Constraints

  • You can only sell on weekdays. You must not sell for two consecutive days if today is the same day as yesterday.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Longest Holiday Free Subsequence — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n)
|
SpaceO(1)

Problem Description

Given an array of strings representing days of the week and an array of strings representing holidays, determine the length of the longest subsequence that does not contain any holidays. A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Longest Holiday Free Subsequence"

medium

WHY DOES IT MATTER?

Recognizing that subsequence selection is unconstrained by adjacency allows the problem to collapse to a simple counting task, avoiding combinatorial explosion.

OPTIMIZATION CHALLENGE

The key insight is that membership testing can be done in constant time with a hash set, turning an exponential brute force into linear time.

REAL-WORLD CONNECTION

It mirrors filtering logs: you keep all entries that don't match a blacklist, which is a single pass over the data stream.

Always ask whether the problem’s constraints (subsequence vs. substring) allow a greedy or counting solution before diving into DP or backtracking.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(1)

Core Theory — Why This Approach?

The problem reduces to counting the number of days that are not holidays, because a subsequence can skip any elements without changing order. A naive approach would generate all 2^n subsequences and check each for the presence of a holiday, which is exponential and infeasible for large n. The optimal paradigm uses a hash set for O(1) holiday lookup and a single linear scan, yielding O(n) time and O(1) additional space.

Interview Questions on This Problem

Q1How would you modify the algorithm if the input were a stream of days instead of an array?

You would maintain a hash set of holidays and a counter. As each day arrives, check membership in the set; if not a holiday, increment the counter. This gives an online O(1) per element solution with O(1) space beyond the set.

Q2In a distributed system where days are partitioned across nodes, how would you compute the longest holiday‑free subsequence efficiently?

Each node counts its local non‑holiday days and returns the count. The coordinator aggregates the counts (sum) to get the global answer. This is a Map‑Reduce pattern with linear time and minimal communication.

Q3What if the definition of a holiday changes dynamically during processing?

Maintain a dynamic set of holidays; when a holiday is added or removed, update the set. The algorithm still scans once, but you may need to recompute if the set changes before the scan completes. For incremental updates, you can adjust the counter on the fly as days are processed.

Examples

Example 1

Input

["Monday", "Tuesday", "Wednesday"], ["Tuesday"]

Output

2

Explanation: Step-by-step: with input ["Monday", "Tuesday", "Wednesday"] and holidays ["Tuesday"], we first identify the holidays in the given array. Then, we find the longest subsequence that does not contain any holidays. In this case, the longest subsequence is ["Monday", "Wednesday"], giving output 2.

Example 2

Input

["Monday", "Tuesday", "Wednesday", "Thursday"], ["Monday", "Wednesday"]

Output

1

Explanation: Step-by-step: with input ["Monday", "Tuesday", "Wednesday", "Thursday"] and holidays ["Monday", "Wednesday"], we first identify the holidays in the given array. Then, we find the longest subsequence that does not contain any holidays. In this case, the longest subsequence is ["Tuesday"] or ["Thursday"], giving output 1.

Constraints

  • You can only sell on weekdays. You must not sell for two consecutive days if today is the same day as yesterday.

Optimal Approach & Strategy

Create a hash set of holidays for O(1) lookups, iterate through the days array once, and increment a counter whenever a day is not in the set. This runs in O(n) time and O(1) extra space.

Brute Force Approach

Generate every possible subsequence of the days array and check each one for the presence of any holiday. This requires O(2^n) time and is impractical for large n.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(days, holidays) { let count = 0; for (let day of days) { if (!holidays.includes(day)) { count++; } } return count; }

Asked in Top Tech Interviews

arraysmediumsliding-window

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.