Algorithms Problem Solving: Number of students
Tags: algorithms, python, computerscience, interview
Published: 2020-06-11 11:07:11

---
title: Algorithms Problem Solving: Number of students
published: true
description: Solving algorithms problems with TK
tags: algorithms, python, computerscience, interview
cover_image: https://raw.githubusercontent.com/leandrotk/tk/master/series/algorithms-problem-solving/assets/classroom.jpg
canonical_url: https://leandrotk.github.io/tk/series/algorithms-problem-solving/number-of-students
series: Algorithms Problem Solving Series
---

This post is part of the [Algorithms Problem Solving](https://leandrotk.github.io/tk/series/algorithms-problem-solving/index.html) series.

## Problem description

This is the [number of students](https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/) problem. The description looks like this:

Given two integer arrays `startTime` and `endTime` and given an integer `queryTime`.

The `ith` student started doing their homework at the time `startTime[i]` and finished it at time `endTime[i]`.

Return *the number of students* doing their homework at time `queryTime`. More formally, return the number of students where `queryTime` lays in the interval `[startTime[i], endTime[i]]` inclusive.

## Examples

```python
Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output: 1

Input: startTime = [4], endTime = [4], queryTime = 4
Output: 1

Input: startTime = [4], endTime = [4], queryTime = 5
Output: 0

Input: startTime = [1,1,1,1], endTime = [1,3,2,4], queryTime = 7
Output: 0

Input: startTime = [9,8,7,6,5,4,3,2,1], endTime = [10,10,10,10,10,10,10,10,10], queryTime = 5
Output: 5
```

## Solution

The idea is just to iterate through the lists and compare them with the `query_time` to see if it is in the interval between start and end time. If it is, just increment the `number_of_students` counter. After the for loop finishes, return this value.

```python
def busy_student(start_time, end_time, query_time):
    number_of_students = 0

    for index in range(len(start_time)):
        start, end = start_time[index], end_time[index]

        if query_time >= start and query_time <= end:
            number_of_students += 1

    return number_of_students
```

But we could also use the `zip` function to iterate through the list simultaneously:

```python
def busy_student(start_time, end_time, query_time):
    number_of_students = 0

    for start, end in zip(start_time, end_time):
        if query_time >= start and query_time <= end:
            number_of_students += 1

    return number_of_students
```

The runtime complexity is `O(N)` where `N` is the number of integers in the `start_time` and `end_time`.

## Resources

- [Learning Python: From Zero to Hero](https://leandrotk.github.io/tk/2017/09/learning-python-from-zero-to-hero/index.html)
- [Algorithms Problem Solving Series](https://leandrotk.github.io/tk/series/algorithms-problem-solving/index.html)
- [Stack Data Structure](https://leandrotk.github.io/tk/2020/01/stack-data-structure/index.html)
- [Queue Data Structure](https://leandrotk.github.io/tk/2020/01/queue-data-structure/index.html)
- [Linked List](https://leandrotk.github.io/tk/2020/02/linked-list/index.html)
- [Tree Data Structure](https://leandrotk.github.io/tk/2020/02/tree-data-structure/index.html)