Index of the remaining student

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-06-26

Problem Statement

Design an object-oriented system that determines the index (position) of the last remaining student after repeatedly eliminating every k-th student from a circular arrangement of n students.


This is a variation of the Josephus Problem, focusing on returning the index (not the student ID or name).


🎯 Objective


Given:

•N — total number of students standing in a circle, numbered 0 to N-1.
•K — every K-th student is eliminated in a circular fashion.


After repeatedly removing every K-th student until only one remains, determine the index of the last remaining student.


Your design should emphasize modularity, reusability, and extensibility, allowing variations like different counting directions or skip patterns.


✅ Functional Requirements


1.The system should accept:
•Total number of students N.
•Elimination step K.
2.The system should simulate the elimination process (or use a mathematical approach) to find the index of the last remaining student.
3.The system should output:
•The final remaining student’s index (0-based or 1-based as configured).
4.Handle edge cases:
•N = 1 → output 0.
•K = 1 → output N−1.
•Large N values (should not cause performance issues).
5.Optional: Provide elimination order for debugging or visualization.


⚙️ Non-Functional Requirements


Aspect with Description


Time Complexity

O(N) or better (use optimized recurrence if possible).


Scalability

Should handle up to millions of students.


Extensibility

Easy to plug in different elimination rules or data structures.


Testability

Each logical component must be unit-testable.


Maintainability

Use clean OOP design principles.


🧱 Key Classes


1. StudentCircle

Represents the circular group of students. Handles traversal and elimination logic.

1 2class StudentCircle { 3 private List<Integer> students; 4 public int removeKthStudent(int k); 5 public boolean hasMoreThanOne(); 6}


2. JosephusSolver

Encapsulates the logic to find the index of the last remaining student using simulation or mathematical recursion.

1 2class JosephusSolver { 3 public int findRemainingIndex(int n, int k); 4}


3. JosephusApp

Driver class to take user inputs and display results.

1 2class JosephusApp { 3 public static void main(String[] args); 4}


4. (Optional) EliminationStrategy

Interface to allow plugging different solving strategies (simulation vs mathematical formula).

1 2interface EliminationStrategy { 3 int getRemainingIndex(int n, int k); 4}


5. (Optional) Implementations

•SimulationStrategy → Uses circular linked list or array simulation.
•RecursiveMathStrategy → Uses recurrence relation:
1 2f(1) = 0 3f(n) = (f(n - 1) + k) % n


🔄 Example Flow


Input:

1 2N = 7, K = 3


Process:

Elimination order → [2, 5, 1, 6, 4, 0]

Remaining student → index 3


Output:

1 2The index of the remaining student is: 3


💾 Possible Extensions


•Allow variable step size K for each round.
•Support reverse counting or random start positions.
•Extend for “every K-th team eliminated” in tournament systems.
•Add visualization of elimination order for simulation mode.


🧠 Key Design Considerations


Use modular strategy pattern:

•Mathematical recurrence for efficiency.
•Simulation strategy for clarity or visualization.
•Keep algorithm independent of data representation (array, linked list, etc.).
•Make the system flexible for different counting or elimination rules.


✅ Expected Output Examples


Input and Output with Explanation


N = 1, K = 1

0


Only one student remains


N = 5, K = 2

2


Remaining index = 2


N = 7, K = 3

3


Remaining index = 3


N = 10, K = 1

9


Always the last index when K = 1


📘 Formula Reference (Mathematical Approach)


Recursive definition for Josephus position:

1 2f(1) = 0 3f(n) = (f(n - 1) + k) % n

Final survivor (0-based index) = f(N)


Solution

Comments