FastPrepOpen or Closed: A Door After the First Y People

Open or Closed: A Door After the First Y People

Capital One logoCapital One● EasyFULLTIMEONSITE INTERVIEW

Updated

Problem statement

A row of 100 doors, numbered 1 to 100, starts with every door closed. For each k in turn, person k walks the row and switches the state of every k-th door (doors k, 2k, 3k, … up to 100): an open door is closed and a closed door is opened.

Return true if door door is open after persons 1 through person have all walked the row, and false if it is closed.

Function

isDoorOpen(door: int, person: int) → boolean

Examples

Example 1

door = 36person = 100return = true

Door 36 is touched by persons 1, 2, 3, 4, 6, 9, 12, 18 and 36: nine times, an odd number, so it ends open.

Example 2

door = 12person = 5return = false

Persons 1 to 5 reach door 12 at 1, 2, 3 and 4 (5 does not divide 12): four touches, an even number, so it is closed again.

Example 3

door = 7person = 1return = true

Only person 1 has passed, and person 1 opens every door, so the answer is true.

Constraints

  • 1 <= door <= 100
  • 1 <= person <= 100

More Capital One Onsite Interview questions

Similar questions at other companies

Capital One interview experiences

All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public boolean isDoorOpen(int door, int person) {
        // Write your code here.
    }
}
Checking account…