Skip to content

Practice Find Students with Study Spiral Pattern

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3617
LeetCode Problem

Find Students with Study Spiral Pattern

Problem Statement

Tables: students(student_id INT, student_name VARCHAR, major VARCHAR) and study_sessions(session_id INT, student_id INT, subject VARCHAR, session_date DATE, hours_studied DECIMAL). Find a rotating sequence of at least three different subjects repeated for at least two complete cycles. Adjacent sessions in the pattern must be no more than two days apart. Return student_id, student_name, major, cycle_length (distinct subjects in the cycle), and total_study_hours across its sessions. Sort by cycle_length descending, then total_study_hours descending. Practice approach: Order sessions by date, identify candidate repeating subject orders, verify each repetition rather than relying only on distinct-subject counts, and check every adjacent date gap. Sum hours only for the validated pattern. The source does not specify how to choose among multiple qualifying patterns or order same-day sessions; those cases need clarification rather than an invented tie-breaker.

Source: Find Students with Study Spiral Pattern on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
{
  "students": [
    {
      "student_id": 1,
      "student_name": "Ava",
      "major": "Science"
    },
    {
      "student_id": 2,
      "student_name": "Bo",
      "major": "Arts"
    }
  ],
  "study_sessions": [
    {
      "session_id": 1,
      "student_id": 1,
      "subject": "Math",
      "session_date": "2026-01-01",
      "hours_studied": 1
    },
    {
      "session_id": 2,
      "student_id": 1,
      "subject": "Physics",
      "session_date": "2026-01-03",
      "hours_studied": 2
    },
    {
      "session_id": 3,
      "student_id": 1,
      "subject": "Chemistry",
      "session_date": "2026-01-04",
      "hours_studied": 1
    },
    {
      "session_id": 4,
      "student_id": 1,
      "subject": "Math",
      "session_date": "2026-01-05",
      "hours_studied": 1
    },
    {
      "session_id": 5,
      "student_id": 1,
      "subject": "Physics",
      "session_date": "2026-01-07",
      "hours_studied": 2
    },
    {
      "session_id": 6,
      "student_id": 1,
      "subject": "Chemistry",
      "session_date": "2026-01-08",
      "hours_studied": 1
    },
    {
      "session_id": 7,
      "student_id": 2,
      "subject": "Art",
      "session_date": "2026-01-01",
      "hours_studied": 2
    },
    {
      "session_id": 8,
      "student_id": 2,
      "subject": "Music",
      "session_date": "2026-01-02",
      "hours_studied": 2
    },
    {
      "session_id": 9,
      "student_id": 2,
      "subject": "Art",
      "session_date": "2026-01-03",
      "hours_studied": 2
    },
    {
      "session_id": 10,
      "student_id": 2,
      "subject": "Music",
      "session_date": "2026-01-04",
      "hours_studied": 2
    }
  ]
}
Output
[
  {
    "student_id": 1,
    "student_name": "Ava",
    "major": "Science",
    "cycle_length": 3,
    "total_study_hours": 8
  }
]
Explanation

Ava completes Math,Physics,Chemistry twice with gaps of one or two days and eight total hours. Bo has only two distinct subjects.

Constraints

students has key student_id; study_sessions has key session_id.
A qualifying cycle has at least three distinct subjects and repeats completely at least twice.
Date gaps within the pattern are at most two days.

Related LeetCode Problems

Practice more problems similar to Find Students with Study Spiral Pattern.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.