FastPrepTree Toggles with Periodic Permanent Locks
Problems/Tree Toggles with Periodic Permanent Locks

Tree Toggles with Periodic Permanent Locks

IMC Trading logoIMC Trading● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an IMC Trading new grad online assessment. The task toggles numbered trees every night and locks active trees for good every thirty nights. Our simulation is about 95% the same. This version lets the three counts change, and says that toggling comes before locking on the same night.

There are n trees numbered from 1 through n. Every tree starts dormant and unlocked.

Process nights 1 through nights in increasing order. On night d, toggle every unlocked tree whose number is a multiple of d: dormant becomes active, and active becomes dormant.

On every night divisible by lockPeriod, permanently lock each currently active tree. A locked tree remains active and cannot be toggled on any later night. Dormant trees are not locked.

For this exercise, assume that a night's toggles happen before its locking event. For this exercise, assume the tree count, night count and lock period generalize the values 225, 225 and 30 used in the first example.

Return the total number of active trees after the final night, including locked active trees. Zero trees or zero nights gives zero.

Complete countActiveTrees(n, nights, lockPeriod).

Function

countActiveTrees(n: int, nights: int, lockPeriod: int) → int

Examples

Example 1

n = 225nights = 225lockPeriod = 30return = 200

Apply the nightly toggles and lock active trees after nights 30,60,90,120,150,180,210. Counting the final states gives 200 active trees. Without locking, only the fifteen perfect-square-numbered trees would remain active; that is a different process.

Example 2

n = 2nights = 2lockPeriod = 2return = 1

Night 1 activates both trees. Night 2 toggles tree 2 back to dormant, then locks active tree 1. Exactly one tree is active.

Example 3

n = 5nights = 3lockPeriod = 1return = 5

Night 1 activates and then permanently locks every tree. Later nights cannot change them.

Example 4

n = 4nights = 0lockPeriod = 2return = 0

No night occurs, so every tree remains dormant.

Constraints

  • For this exercise, assume 0 <= n <= 2000, 0 <= nights <= 2000 and 1 <= lockPeriod <= 2000.
  • Tree numbers and night numbers are one-based.
  • A lock is permanent and applies only to a tree active immediately after that night's toggles.

More IMC Trading Online Assessment questions

  • Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
  • Grid Paths with Override PassesOA · Seen Oct 2026
  • Weighted LFU CacheOA · Seen Sep 2026
  • Rock JumpingOA · Seen Aug 2026
  • Stack Batch RemovalOA · Seen Aug 2026
  • Chain of CommandOA · Seen Aug 2026
  • Choose ContainersOA · Seen Aug 2026
  • Sub-matrix SumsOA · Seen Jul 2026
  • Sub-matrix SumsOA · Seen Jul 2026
  • Can Every Package Fit in a BoxOA · Seen Jul 2026

Similar questions at other companies

  • Last Object Exit TimeIBM · OAIBM interview questions
  • Capping Portfolio WeightsCapstone Investment Advisors · OA
  • Bold Play to Reach a GoalSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Repeated Mirror XORMathWorks · OAMathWorks interview questions
  • Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
  • Find Total CostJP Morgan Chase · OAJP Morgan Chase interview questions
  • Macaron Order PriceOpenAI · OAOpenAI interview questions
  • Daily Settlement for Long and Short PositionsBelvedere Trading · OA
  • Debug the Risk-Limit QuoterMillennium · OAMillennium interview questions
  • Prime Factor VisitationMaven Securities · OAMaven Securities interview questions

Interview reports that mention this question

  • IMC Trading OA: What It Tests (2026)
  • IMC SWE New Grad OA and Interviews: 58 Experiences (Part 1 of 3)

IMC Trading interview experiences

  • IMC Trading SWE OA and Video: 18 Experiences (Part 3 of 3)
  • IMC Trading SWE OA Questions: 22 Experiences (Part 2 of 3)
All IMC Trading interview questionsSee IMC Trading hiring insights
Saved
class Solution {
    public int countActiveTrees(int n, int nights, int lockPeriod) {
        // Write your solution here.
    }
}
Checking account…