• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need debugging help (What is the issue with this code?)  RSS feed

 
Alex Tri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked to correct the below code, I'm not sure what is the issue with this code. After the code i have given the complete instruction or description for given issue.


The algorithm

This purposely badly written code simulates the setting up and execution of multiple streams of work, each of which is assigned an equally-sized team of contractors out of a general, large enough pool of contractors.

Each stream of work has to go through an equal number of tasks, which are assigned to contractors in its team for execution.

Any contractor in the general pool of contractors is assigned a sub-contractor, out of the same pool - to help whenever the load of work is too high (hardcoded as > 3 tasks).
If possible, this will be a contractor not already assigned to any stream of work:  a sub-contractor will always be assigned, however.

Sacred constraints

Streams of work and tasks are and must be all executed concurrently (that is, to each its own thread).

The contractors at hand do and must enforce these constraints:

• Cannot work (“do their magic”) on more than one task at any given time; they can accept more than one task to work on or to sub-contract, but if carrying out tasks on their own they must go through them sequentially
• Contractors must not carry out more than 3 tasks on their own. However, if (and only if) their sub-contractor (and its sub-contractors) have no capacity left (they are all busy with 3 or more tasks), then they must execute additional task on their own (regardless of how many tasks in total)

Input parameters

The code accepts the following as input parameters:

1. Size of the pool of contractors
2. Number of streams of work
3. Number of tasks per stream of work
4. Teams size

Such numbers are checked for coherence only (i.e. the pool of contractors must be large enough to provide for all teams, etc.)

E.g.: java test.Ouch 2 8 12 3


Number of contractors: 12
Number of streams of work: 2
Number of tasks per stream of work: 8
Teams size: 3


The problem

Running this code with valid parameters may not work as expected!!


What we ask you

Without focusing on efficiency and optimization:

1. What might be the reason why running this code could result in an indefinitely hanging process? What are the circumstances under which the problem might materialize, and why?

2. By touching the code as little as possible, and keeping in mind our “sacred constraints”, how would you address the problem?
 
Tim Moores
Saloon Keeper
Posts: 4034
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

That looks like an exercise you're supposed to be doing in order to learn. So: what ideas have you had? What, specifically, do or do you not understand about the code and the description?
 
Alex Tri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. as per given instruction, they mentioned that there is an issue with the code and need to correct but I couldn't find any issue. I am not sure what to do so seeking help from expert who can analyze the given problem and suggest solutions.
 
Liutauras Vilda
Sheriff
Posts: 4922
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Tri wrote:as per given instruction, they mentioned that there is an issue with the code and need to correct

1. First verify if code compiles.
2. Verify if code has functionality requested in specification.
3. Verify if code gives expected output.

You can almost ignore 1, 2 and 3, it seems you are asked specific questions.
Task wrote:1. What might be the reason why running this code could result in an indefinitely hanging process? What are the circumstances under which the problem might materialize, and why?

2. By touching the code as little as possible, and keeping in mind our “sacred constraints”, how would you address the problem?
So, FIRST, you need to identify the circumstances at which code wouldn't work as expected, "would possibly hang", and SECOND, how would you solve that issue, so it wouldn't happen.

Do you understand in general what this program is supposed to be doing? That is probably should be the starting point - to analyse code and be sure you understand what it does, basically each line, then think about the corner cases on which possible inputs program would misbehave.

Now in order to identify the corner cases, you need to read about program's functionality, which it seems you have been give too, and sounds like you could possibly find some hints in there where to start looking.

Try to tackle scenarios opposite to what specification says. For instance, "Contractors must not carry out more than 3 tasks on their own", so think if there would be something wrong if there would 10 tasks.
It is a dummy example I gave, but it is one of possibility how you could start looking to it.

Now I don't know answer myself, but this is what I'd be doing if I were given such task.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides knowing what the overall behavior of the program should be, you also need to understand the behavior expected from specific methods. I spotted a potential problem just by reading and understanding what one method is doing and not being able to reconcile it with some of the constraints given. I would still need to test my theory though.

Bottom line: You have to understand both what the required behavior is and what the actual behavior will be in order to recognize a potential difference between the two.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to add that I think this is an excellently formulated exercise. Kudos to your instructor for giving this kind of challenge. Please feel free to tell your instructor that someone on the internet said so, too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!