Bird
Raised Fist0
Apache Airflowdevops~10 mins

Timetables for complex schedules in Apache Airflow - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a daily timetable starting at midnight.

Apache Airflow
from airflow.timetables.base import Timetable
class DailyMidnightTimetable(Timetable):
    def infer_manual_data(self, run_after: float):
        return run_after
    def next_dagrun_info(self, last_automated: float | None, now: float) -> float:
        return [1]
Drag options to blanks, or click blank then click option'
Alast_automated + 3600
Bnow + 86400
C(now // 86400 + 1) * 86400
Dnow
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'now + 86400' which schedules at the same time next day, not midnight
Using 'last_automated + 3600' which is hourly from last run
Returning 'now' for immediate execution
2fill in blank
medium

Complete the code to create a timetable that triggers every Monday at 9 AM.

Apache Airflow
from airflow.timetables.base import Timetable
from datetime import datetime, timedelta
class WeeklyMonday9AMTimetable(Timetable):
    def next_dagrun_info(self, last_automated: float | None, now: float) -> float:
        next_run = datetime.fromtimestamp(now).replace(hour=9, minute=0, second=0, microsecond=0)
        while next_run.weekday() != [1] or next_run.timestamp() <= now:
            next_run += timedelta(days=1)
        return next_run.timestamp()
Drag options to blanks, or click blank then click option'
A0
B4
C6
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1, which is Tuesday, not Monday
Confusing with systems where Sunday is 0
3fill in blank
hard

Fix the error in the code to correctly calculate the next run time for a timetable that triggers every hour on the half hour.

Apache Airflow
from airflow.timetables.base import Timetable
from datetime import datetime, timedelta
class HourlyHalfHourTimetable(Timetable):
    def next_dagrun_info(self, last_automated: float | None, now: float) -> float:
        current = datetime.fromtimestamp(now)
        next_run = current.replace(minute=30, second=0, microsecond=0)
        if current.minute >= 30:
            next_run += timedelta(hours=[1])
        return next_run.timestamp()
Drag options to blanks, or click blank then click option'
A0
B1
C30
D60
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 0 hours, leaving next_run in the past
Confusing minutes with hours (30 or 60)
4fill in blank
hard

Fill both blanks to create a timetable that triggers every weekday at 8 AM.

Apache Airflow
from airflow.timetables.base import Timetable
from datetime import datetime, timedelta
class Weekday8AMTimetable(Timetable):
    def next_dagrun_info(self, last_automated: float | None, now: float) -> float:
        next_run = datetime.fromtimestamp(now).replace(hour=[1], minute=0, second=0, microsecond=0)
        while next_run.timestamp() <= now or next_run.weekday() [2] 5:
            next_run += timedelta(days=1)
        return next_run.timestamp()
Drag options to blanks, or click blank then click option'
A8
B<
C>=
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Setting hour to 7 or other value instead of 8
Using '< 5' which skips weekdays (Mon-Fri) instead of weekends
5fill in blank
hard

Fill all three blanks to create a timetable that triggers on the first day of each month at midnight.

Apache Airflow
from airflow.timetables.base import Timetable
from datetime import datetime, timedelta
class MonthlyFirstDayMidnightTimetable(Timetable):
    def next_dagrun_info(self, last_automated: float | None, now: float) -> float:
        current = datetime.fromtimestamp(now)
        year = current.year
        month = current.month + [1]
        if month > 12:
            month = [2]
            year += 1
        next_run = datetime(year, month, [3], 0, 0, 0)
        return next_run.timestamp()
Drag options to blanks, or click blank then click option'
A1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 for day (invalid date)
Failing to reset month to 1 or increment year on overflow
Adding 0 instead of 1 to month

Practice

(1/5)
1. What is the main purpose of a timetable in Apache Airflow?
easy
A. To control when workflows (DAGs) are scheduled to run
B. To store data processed by workflows
C. To define the tasks inside a workflow
D. To monitor the status of running tasks

Solution

  1. Step 1: Understand Airflow scheduling

    Airflow uses timetables to decide when to start running workflows (DAGs).
  2. Step 2: Differentiate timetable from other components

    Timetables do not store data, define tasks, or monitor tasks; they only schedule runs.
  3. Final Answer:

    To control when workflows (DAGs) are scheduled to run -> Option A
  4. Quick Check:

    Timetable = schedule control [OK]
Hint: Timetables decide when workflows start running [OK]
Common Mistakes:
  • Confusing timetable with task definitions
  • Thinking timetable stores data
  • Mixing timetable with monitoring features
2. Which of the following is the correct way to import the built-in cron timetable in Airflow 2.5+?
easy
A. import airflow.timetables.Cron as CronTimetable
B. import airflow.timetable.cron as CronTimetable
C. from airflow.schedules import CronTimetable
D. from airflow.timetables.cron import CronTimetable

Solution

  1. Step 1: Recall Airflow timetable import paths

    Airflow 2.5+ uses airflow.timetables.cron for cron timetable classes.
  2. Step 2: Check syntax correctness

    from airflow.timetables.cron import CronTimetable uses correct module and class name with proper Python import syntax.
  3. Final Answer:

    from airflow.timetables.cron import CronTimetable -> Option D
  4. Quick Check:

    Correct import path = from airflow.timetables.cron import CronTimetable [OK]
Hint: Use airflow.timetables.cron for cron timetable imports [OK]
Common Mistakes:
  • Using wrong module name like 'schedules' instead of 'timetables'
  • Incorrect capitalization in module or class names
  • Wrong import syntax causing errors
3. Given this Airflow timetable code snippet, what schedule does it represent?
from airflow.timetables.cron import CronTimetable
cron = CronTimetable('0 6 * * 1-5')
medium
A. Runs every day at 6:00 AM
B. Runs every hour from 6 AM to 5 PM
C. Runs at 6:00 AM every weekday (Monday to Friday)
D. Runs only on weekends at 6:00 AM

Solution

  1. Step 1: Interpret the cron expression '0 6 * * 1-5'

    The fields mean: minute=0, hour=6, day=any, month=any, weekday=1-5 (Monday to Friday).
  2. Step 2: Match cron to schedule meaning

    This means the job runs at 6:00 AM on weekdays only.
  3. Final Answer:

    Runs at 6:00 AM every weekday (Monday to Friday) -> Option C
  4. Quick Check:

    Cron '0 6 * * 1-5' = Weekday 6 AM [OK]
Hint: Cron weekdays 1-5 means Monday to Friday [OK]
Common Mistakes:
  • Assuming it runs every day instead of weekdays only
  • Confusing hour and minute fields
  • Thinking it runs on weekends
4. You wrote this custom timetable code but Airflow raises an error:
class MyTimetable:
    def __init__(self, cron):
        self.cron = cron

my_tt = MyTimetable('0 0 * * *')

What is the likely cause of the error?
medium
A. Not importing CronTimetable from airflow
B. Missing required methods like 'next_dagrun_info' in the timetable class
C. Using a class instead of a function for timetable
D. Incorrect cron string format

Solution

  1. Step 1: Understand Airflow timetable requirements

    Custom timetables must implement specific methods like 'next_dagrun_info' to work.
  2. Step 2: Check provided class

    The class only stores cron string but lacks required methods, causing Airflow error.
  3. Final Answer:

    Missing required methods like 'next_dagrun_info' in the timetable class -> Option B
  4. Quick Check:

    Custom timetable needs required methods [OK]
Hint: Custom timetables must implement Airflow's required methods [OK]
Common Mistakes:
  • Assuming any class with cron string works as timetable
  • Blaming cron string format without checking methods
  • Confusing timetable class with task definitions
5. You want to run a DAG every Monday at 8 AM and also on the 1st day of every month at 9 AM. Which Airflow timetable setup correctly combines these schedules?
hard
A. Use a custom timetable that combines two CronTimetable instances for '0 8 * * 1' and '0 9 1 * *', then merges their schedules
B. Create two DAGs, one with '0 8 * * 1' and another with '0 9 1 * *', then combine their runs manually
C. Use a single CronTimetable with expression '0 8 * * 1,0 9 1 * *'
D. Set schedule_interval='@weekly' and add a sensor to trigger on the 1st day of month

Solution

  1. Step 1: Understand the scheduling needs

    We need two different schedules: Mondays at 8 AM and 1st day of month at 9 AM.
  2. Step 2: Evaluate options for combining schedules

    A single cron expression cannot represent both times simultaneously. Two DAGs is possible but not ideal. A custom timetable combining two CronTimetable instances is the correct approach.
  3. Step 3: Why other options fail

    Use a single CronTimetable with expression '0 8 * * 1,0 9 1 * *' is invalid cron syntax. Create two DAGs, one with '0 8 * * 1' and another with '0 9 1 * *', then combine their runs manually is manual and complex. Set schedule_interval='@weekly' and add a sensor to trigger on the 1st day of month uses sensors which is indirect and less clean.
  4. Final Answer:

    Use a custom timetable that combines two CronTimetable instances for '0 8 * * 1' and '0 9 1 * *', then merges their schedules -> Option A
  5. Quick Check:

    Combine schedules with custom timetable = Use a custom timetable that combines two CronTimetable instances for '0 8 * * 1' and '0 9 1 * *', then merges their schedules [OK]
Hint: Combine multiple cron schedules with a custom timetable class [OK]
Common Mistakes:
  • Trying to put two cron expressions in one string
  • Creating multiple DAGs instead of one with combined timetable
  • Using sensors instead of proper timetable combination
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard