An introduction to Ad-Hoc

March 4, 2018

What are Ad-Hoc Challenges ?


One may search the term Ad-Hoc in Google and end up getting the answer “created or done for a particular purpose as necessary”.

Indeed, it is. Ad-Hoc programming questions take up no other meaning than the meaning already described above. In order to solve an Ad-Hoc question, you will have to forget all algorithms and data structures – Binary Search, Dynamic Programming, Trees, Graphs and just focus on finding the true meaning of the question.

Every question of this type has very a specific method to solve, a method “created or done for a particular purpose as necessary”. Your job as a problem solver is to find that method.


The event MindSweeper of Jontro Tontro ’18 will consist of 4 such Ad-Hoc Challenges to be solved within 2 hours. And as a problem setter for the event, I can guarantee that all the questions can be solved within 2-3 lines of the main code.

In order to participate in this event, you do not need to know any kind of Data Structures or Algorithms nor any kind of fancy tricks with a programming language. All you need to know is how to take input and how to output your answer using any programming language. Another important thing that you will need is the enthusiasm for solving logical puzzles.


See, you on the Leaderboard!


REGISTER by clicking here (only for first year undergraduates)



Here is an example of an Ad-Hoc question from Codechef’s February Long Challenge:


Problem Name: Car-pal Tunnel

Problem Statement: There is a straight road that passes through N short tunnels. At the entrance to the first tunnel, there are C cars lined up in a row waiting to enter the series of tunnels. The distance between each pair of consecutive tunnels is D metres (the lengths of each tunnel and each car are negligible) and the velocity of each car is S metres per second.

Each tunnel contains a toll booth. In the i-th tunnel (1 ≤ i ≤ N), processing a car at the toll booth takes Ai seconds. Only one car can be processed at the same time. If there are multiple cars in the tunnel, they all have to wait for the first car to be processed; afterwards, the first car exits the tunnel, the second car starts being processed and all remaining cars have to wait again, etc. Whenever a car arrives in a tunnel, it has to wait at that tunnel's toll booth until all previous cars have been processed and then get processed itself.

The road and the tunnels are too narrow, so cars can't overtake each other at any time during the journey.

Chef is after Reziba once again. Reziba's car is the first car to enter the tunnels, while Chef's car is the last car to enter the tunnels.

Compute the final delay (in seconds) between the first (Reziba's) car and the last (Chef's) car immediately after Chef's car exits the last tunnel, i.e. after all the cars have passed through the tunnels. This delay is equal to the distance between the first and the last car divided by the cars' velocity, or equivalently to the difference between the times when the last car and the first car exit the last tunnel.



  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.

  • The first line of each test case contains a single integer N denoting the number of tunnels.

  • The second line contains N space-separated integers A1, A2, ..., AN denoting the time taken for processing a car at each toll booth.

  • The third line contains three space-separated integers C, D and S.



For each test case, print a single line containing one real number — the time delay between the first and last car after all the cars have passed through all the tunnels. Your answer will be considered correct if its absolute error is less than 10-6.



  • 1 ≤ T ≤ 100

  • 1 ≤ N ≤ 10^5

  • 1 ≤ Ai ≤ 10^9 for each valid i

  • 2 ≤ C ≤ 10^6

  • 1 ≤ D, S ≤ 10^9




Subtask #1:

  • 1 ≤ N ≤ 1,000

  • C = 2

Subtask #2: Original Constraints






2 2 2

3 5 5


3 2

2 1 1







Example case 1: Each car takes 5/5 = 1 second to go from one tunnel to the next tunnel.

Since each car has to wait for equally long (2 seconds) in each tunnel, the delay between every two consecutive cars exiting each tunnel is 2 seconds.

As there are 3 cars, the total time delay between the first and the last car after the last tunnel is 4 seconds.


The problem statement is really long and boring and misleading, but the answer is a one liner:

Let's say that x is the maximum time taken for processing a car at a tolbooth from the n given values.


Answer: print(x*(c-1))


Pretty easy, isn’t it!!

Find out how this happens.




Share on Facebook
Share on Twitter
Please reload

Jadavpur University Campus, Jadavpur, Kolkata, West Bengal 700032, India

©2020 by Jadavpur University Science Club - JUSC

  • White Facebook Icon
  • White Instagram Icon
  • White Twitter Icon
  • White YouTube Icon
  • White Google Places Icon