Minimum number of platforms required

You have been given two arrays, 'AT' and 'DT', representing the arrival and departure times of all trains that reach a railway station.

Your task is to find the minimum number of platforms required for the railway station so that no train needs to wait.

Note :
1. Every train will depart on the same day and the departure time will always be greater than the arrival time. For example, A train with arrival time 2240 and departure time 1930 is not possible.

2. Time will be given in 24H format and colons will be omitted for convenience. For example, 9:05AM will be given as "905", or 9:10PM will be given as "2110".

3. Also, there will be no leading zeroes in the given times. For example, 12:10AM will be given as “10” and not as “0010”.
Input Format
The first line of input contains an integer 'T' representing the number of test cases.

The first line of each test case contains an integer 'N', representing the total number of trains.

The second line of each test case contains 'N' single-spaced separated elements of the array 'AT',  representing the arrival times of all the trains.

The third line of each test case contains 'N' single-spaced separated elements of the array 'DT', representing the departure times of all the trains.
Output Format:
For each test case, return the minimum number of platforms required for the railway station so that no train needs to wait.
Note :
You don't need to print the output, it has already been taken care of. You just need to implement the given function.
Follow up :
Try to solve the problem in O(N) time and O(1) space.
Constraints:
1 <= T <= 10
1 <= N <= 50000
0 <= AT[i] <= DT[i] <= 2359

Where 'AT[i]' and 'DT[i]' are the elements of the arrival and the departure arrays respectively.

Time limit: 1 sec
AnswerBot
1y

This question asks to find the minimum number of platforms required at a railway station so that no train needs to wait.

  • Sort the arrival and departure times arrays in ascending order.

  • Initialize a vari...read more

CodingNinjas
author
2y

I was able to solve half of it, and the interviewer shifted to next problem.
As I applied for the Frontend role so interviews were not that dependant on DS.

CodingNinjas
author
2y
Using an additional array

The main idea behind this approach is to use an array, 'PLATFORMS', to store the number of platforms required at different points of time. Now, since the time range is from 0...read more

CodingNinjas
author
2y
Using sorting

The main idea behind this approach is to consider all the events in the sorted order. Once the events are in the sorted order, we find the number of trains at any time by keeping track of...read more

CodingNinjas
author
2y
Using cumulative sum

The main difference between this approach and the previous one is that instead of looping through the 'PLATFORMS' array for every train, we just increment the number of platforms a...read more

Add answer anonymously...
Paytm Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter