• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Interview Question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all just got back from a java interview, didn't go very well unfortunately . I took a picture of the question in the hopes of understanding how to solve it but I still cant wrap my head around it, can anyone provide the solutions.

You are given an array on integers n length n for a driver. The integer at index i symbolizes the time allocated to her or his i'th task in minutes. The optimal time for a break s between two tasks i and i+1, when the sum of all tasks from 0 to i equals the sum i+1 to n .

Write a method that returns index i where the condition above is met. If there is no such index the driver is not awarded a break and the method returns null.

Example
Input [1, 4, 1, 3, 1] -> Output 1
Input [1, 4, 1, 3] -> Output null

Bonus:

Can you find a solution that efficiently handles 3, 4, ..., m breaks ?

Thanks in advance!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Figure out how to do this by hand first. Once you understand the process, figuring out an equivalent way to do it in code is usually much easier.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jameson Locke wrote:. . . Write a method that returns index i where the condition above is met. If there is no such index the driver is not awarded a break and the method returns null. . . .

Have you copied the question correctly? Those two instructions are mutually contradictory.
 
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the way you solve it is you first find the sum of all of the numbers.  Then you iterate through creating a sum for the left side and subtracting from the main sum.  When those numbers are the same return the index or return null at the end.


To solve these problems you really gotta just practice it and overtime your brain gets used to solving them.   At first it will be very hard but just give yrself time and grind
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually when the left side is bigger than the main sum you can return null at that point too
 
Jameson Locke
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright been trying to do this for a while now and ended up with this, there's still some errors in the logic though. Would anyone recommend a better way or improvements upon this ?

i
 
Jameson Locke
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry last bit isn't right



Not sure what I'm doing wrong at this point. Pretty annoying question.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm looks like youre doing it recursively which you dont need to do.    Try to write code based on what i said and it will work i think
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i guess youre not doing it recursively but looks like you arent summing the right side
 
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not even sure what this question means.  I guess I'm not ready to start interviewing for Java.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its more clear if you look at the sample output.   When the sum of the left side including the current index equals the right side, you return that index
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this was for an interview, this feedback might be a little more frank than what I'd normally give to a student/beginner. That said, some of this might be a bit painful but don't take the criticism too hard. As painful as some of it may be, I think it's better to call you out on those things so you can think of what areas in your practice you might make an effort to improve and develop better habits that will give you a better chance at doing well on future job interviews like this.

The code is poorly formatted. If you can't take care of small things like keeping code properly indented and aligned, then as an evaluator, I'd have to wonder what other things don't you do to keep your code clean and readable. Reading through the code doesn't give me a sense that you pay much attention to details and that would be a deal breaker for me.

The names you chose are not expressive. The algorithm/approach is not clearly expressed by the code. The code is not very testable either and it doesn't come with unit tests. Overall, this code shows poor coding habits, a lack of organization, and most importantly, fails to solve the problem.

Here's what I would have laid out as my approach:


I would have written tests for scenarios such as:
1. Only one task, should return null
2. Multiple tasks, odd total number of hours should return null
3. Multiple tasks, even number of hours but no way to split evenly should return null
4. ... etc.

Here's what my first-cut solution might have looked like: https://repl.it/@jlacar/Break-Time

[Edit: as of this edit, you'll no longer see my actual first-cut solution because I have since refactored that code several times]
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would putting unit tests in a interview question be appropriate?  Also  if you do the unit tests would you just put them in the main of the class?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an evaluator, I would look for programmers with good development habits. Unit testing is one of them and I would appreciate any effort to show that, even if it were just a hand-rolled unit test harness like what I showed in my example. It would be better if it were done in something like JUnit. For me, it would be best if I saw evidence of a disciplined approach like TDD being practiced. So, yes, for me it would be totally appropriate and even advantageous to include unit tests.

If I were a candidate and the evaluator disapproved of me including unit tests in my answer, then that's not an organization I would be interested in joining anyway.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you would really hate working where i work.   Absolutely no unit tests in sight and lots of copying pasting.    I personally strive to improve myself so i use git and i make unit tests and try to improve my oop theory all the time.  I want to be able to eventually move to a company where i can actually learn from my seniors.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Luckily, I seem to be at a point in my career as a software developer where I have some room to choose who I work for and with whom I work. If I don't think the culture of an organization is a fit for the way I like to work, I still have choice to walk away.

I have spent many years studying and practicing to improve my abilities and I'd rather work at places and people with whom I can share my knowledge and skills and continue to learn and improve as well. That's pretty much why I hang out here a lot.
 
Al Hobbs
Bartender
Posts: 667
14
TypeScript Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i learn alot from here.  Lots of good developers on here
 
Ranch Hand
Posts: 72
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
As an evaluator, I would look for programmers with good development habits


For students or novices who want to cultivate good habits when solving problems and writing code,
How could we know the best approach for a specific problem solving ?

If any suggestions for books or resources who guide us for "what to do" and "what to not do" in specific situations, it would be much appreciates.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's this book I read when I was young, now it's summarized in Wikipedia: How to Solve It.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like the book Think Like a Programmer by V. Anton Spraul
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many other books you can refer to for good advice on programming. Don't look to books that focus on teaching you language syntax though. Look at this article for a list books that I recommend: https://www.linkedin.com/pulse/effective-tdd-10-books-get-you-started-junilu-lacar?trk=portfolio_article-card_title
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to ask, is this a typical Java interview question?  If so, I'm way off from being ready for the first interview, because I don't even know what this question is asking.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a fairly simple problem that I would expect someone applying for even a junior developer position should at least be able to create a working solution for. Otherwise, that person will need more basic training if you decide to bring them on. If you can't even wrap your head around the question then no, you're probably not ready for that first interview.

What part of the problem do you not understand or do you not understand it at all?

By the way, I was giving some training earlier this week at a client and used a variation of this problem as an exercise. Don't feel too bad about not being able to fully comprehend the problem because even the folks I was training had a lot of questions. Feel free to ask about your own doubts.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:I have to ask, is this a typical Java interview question?  If so, I'm way off from being ready for the first interview, because I don't even know what this question is asking.


Nathan, my Dad once said to me (when I was struggling with math homework exercise for primary school), if you don't understand what is being asked, read it through 100 of times until you do understand.

You can minimze that number significantly if you take apart all what is being asked and start sketching out.

Instructions wrote:[1]You are given an array on integers n length n for a driver. [2]The integer at index i symbolizes the time allocated to her or his i'th task in minutes. [3]The optimal time for a break s between two tasks i and i+1, [4]when the sum of all tasks from 0 to i equals the sum i+1 to n .

[5]Write a method that returns index i where the condition above is met. If there is no such index the driver is not awarded a break and the method returns null.


[1] So it says, that you (as a developer) are given an array of numbers (n length means - you don't know how many of them, could be 1 or 2 or 3 or 4 or more), for example [1, 4, 1, 3, 1], and those numbers denote the task times for some hyphothetical driver. Imagine maybe a taxi driver.

[2] [1, 4, 1, 3, 1] <-- so each individual integer (type of number, a whole number, without the fraction) represents a task time in minutes. So first task takes 1 minute to do. Second task takes 4 minutes to do, third task takes 2 minute to do, and so on.

[3] So here it talks about optimal time for break between the tasks lets say second and third, example: [1, 4, <break> 1, 3, 1]

[4] And what is really meant here, that optimal time for a break is, when you have finished some tasks, and those tasks took half of the time of all tasks time. Or in different words let's say you finished tasks which took 4 minutes and you still got other tasks to do which also will take 4 minutes to do. Sort of in a half way through. Example [1, 4, <tasks on the left 5 minutes = tasks on the right also 5 minutes> 1, 3, 1]

Is it clearer?
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:It's a fairly simple problem that I would expect someone applying for even a junior developer position should at least be able to create a working solution for. Otherwise, that person will need more basic training if you decide to bring them on. If you can't even wrap your head around the question then no, you're probably not ready for that first interview.

What part of the problem do you not understand or do you not understand it at all?

By the way, I was giving some training earlier this week at a client and used a variation of this problem as an exercise. Don't feel too bad about not being able to fully comprehend the problem because even the folks I was training had a lot of questions. Feel free to ask about your own doubts.



I don't understand how to begin it, because I don't get what it is asking.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Is it clearer?



Not really.  I took advanced math courses in college and I don't get what the problem is even asking to begin with.  I guess I need to read it a few more times.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it a little more.  I just don't really know what is meant by the word driver here.  Is it supposed to mean anything specific?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:I just don't really know what is meant by the word driver here.  Is it supposed to mean anything specific?



To me a "driver" is a person who sits in front of a steering wheel and causes a vehicle to move through streets. That was Liutauras's interpretation too.

However I used to work for a wholesale distributor where our delivery drivers were an important part of the business. So maybe that's just my bias -- the question in the original post just mentioned "drivers" who had "tasks", which is pretty vague. (And also where I worked our drivers didn't have "tasks" and their breaks didn't work anything like this array of integers.)

So yeah. I guess the "drivers" were put in there to connect this problem to real life. A lot of people can't deal with abstract problems. So you're right, that idea didn't really work too well.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't read any of the posts in detail, but I just wanna see if my understanding is correct conceptually.  I don't know exactly where it said this in the question, but your explanation says you are trying to find out after which index the break occurs where the time between two sides is equal.  Correct?  

If it was me, I would take the sum of all the numbers, and then I would go through a loop, and add all of them together.  When we get to the point of the loop where it is half of the sum, that is the index of the break.  Did I get that correct?  I didn't read any of the other solutions, so I am not copying or repeating what anyone else said if they offered this solution.  I just wanna make sure I get it.  
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think that would be a working solution; somebody else has already suggested it.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, I think that would be a working solution; somebody else has already suggested it.




I didn't read it.  I was just making sure I understood it myself.  The problem wasn't my lack of skills then.  It was just not understanding what the problem was asking.  

The word driver threw me off, because I was thinking installing drivers on your computer.  
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:

Campbell Ritchie wrote:Yes, I think that would be a working solution; somebody else has already suggested it.




I didn't read it.  I was just making sure I understood it myself.  The problem wasn't my lack of skills then.  It was just not understanding what the problem was asking.  

The word driver threw me off, because I was thinking installing drivers on your computer.  


The thing is, that problems usually are not being described in step-by-step algorithm. That is a goal - to come up with one.

When you get problems described in real life, they sound pretty much like that. Most important in my opinion is - is to ask questions when you are in doubt and not assume something, what may or may not be correct.

If somebody would have said to you, find an index of a given array, from which summing up the numbers to the left of the given index and to the right of the given index those sums would be equal - that is an actual algorithm. I trust only 1 out of more (than one).


So it didn't make any clearer for you when I described to you how I perceive these instructions. But that is normal perhaps as different people perceive explanations differently, and possibly only different phrases make "aha" moment to certain individuals - that is the main reason why many of us here explain same things over and over again just in different words, so each of us could absorb what fits them better.
 
Ahmed Ibrahim
Ranch Hand
Posts: 72
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:There's this book I read when I was young, now it's summarized in Wikipedia: How to Solve It.


Junilu Lacar wrote:There are many other books you can refer to for good advice on programming. Don't look to books that focus on teaching you language syntax though. Look at this article for a list books that I recommend: https://www.linkedin.com/pulse/effective-tdd-10-books-get-you-started-junilu-lacar?trk=portfolio_article-card_title



Thank you so much for your valuable advice
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic