r/learnjava • u/IamPilgrim • May 15 '18
Was provided a coding challenge for a potential jobopportunity. Was rejected based on the code. Looking to improve.
As described above, I was asked to complete a coding challenge to proceed to the next round. I have 2 years of professional experience in developing web applications using the Java stack.
The problem seemed simple enough, you can see the problem and my solution on my Github: https://github.com/darshitpp/RealTimeStats
I was however rejected as I fell short of what they expected from a candidate. I asked for specific feedback, and they obliged.
This was our Developer's feedback:
Low Test coverage
No Constant memory
Side effects in the verify method
Recalculating max over entire list of transactions on every add, taking too much time
I admit there are some mistakes I made, namely
Low Test coverage
This was the first time that I wrote tests (mainly because we never write Automated tests in our org and I never learnt it any time). My question here is how to determine your test coverage for an API? I also had difficulties in learning how to write tests for the API using JUnit. What to test? What not to? And how to test? Can anyone point me to some resources to read up on this?
No Constant memory
They were asking for a constant memory implementation for the GET method, and I did it using an ExpiringMap and static variables (please see my github). I couldn't have done it better. Maybe I misunderstood something?
Side effects in the verify method
I can re-check this myself, but it there is something out of the ordinary, please suggest where I'm wrong.
Recalculating max over entire list of transactions on every add, taking too much time
The approach I took for calculating the max was the best I could think of, since the transactions constantly expire after 60 seconds. Calculating max by storing the previous max value would have been not reasonable as transactions expire.
Anything else I should look out for and things I should keep in mind for the next time I encounter a coding test from a company?
My org doesn't follow the best practices they should, so it's very probable that I've missed something obvious to people from other companies. My confidence as a good programmer has taken a beating. Learning by asking is the only thing I can do at the moment.
Thanks for all your help.
7
u/mladensavic94 May 15 '18
Read about Test Driven Development. Start from that and when you understand the need for test, move to JUnit and Mockito tutorials.
Cant comment since i dont understand requirement completely
Your method also changes other fields thus generating side effect. Learn about 'pure functions' to better understand it.
Indeed it is too long because is requires O(n) in average. Plus you are doing it twice (max and min) for every add. Having Atomic's is hard for processing. My suggestion would be to keep concurrent list of transactions and when stat's are required to calculate sum,avg,min and max all in O(n) time.
Also i have 2 questions for you.
How the fu*k do you test your products in your company? On production?
In StatsDaoImpl.java * Executes in O(1) time and space as per requirement * did they really asked you to implement statistics check in O(1)?? How the fu*k is that possible.
4
u/joshuaherman May 15 '18
Any recommended books on TDD?
1
u/mladensavic94 May 17 '18
I was suggested to read "Test-Driven Development by Example" by Kent Beck. Never actually got it because lack of time, i used blog posts and existing code to learn.
1
u/IamPilgrim May 15 '18
when you understand the need for test
I do understand the need, though I am confused at what breadth and depth to test code. I will read up on TDD.
My suggestion would be to keep concurrent list of transactions and when stat's are required to calculate sum,avg,min and max all in O(n) time.
That'd be my solution precisely, before they expected me to provide a O(1) solution to the generation of statistics. Is there actually a way to have both the requirements satisfied?
How the fuk do you test your products in your company? On production?
The less said, the better. There is manual testing (by providing expected input and verifying the output with the business guys if it matches. No JUnit tests are expected, nor encouraged. There is no unit/integration testing, we are just expected to code solutions to the bugs on the fly as they are discovered when the business team discovers unexpected output based on different input criteria. Shitty practice as hell. And this is the subsidiary of one of the Fortune company lists. The last project actually took us more time to test and fix bugs, than actual development(because all bugs are discovered during functional testing).
Executes in O(1) time and space as per requirement did they really asked you to implement statistics check in O(1)?? How the fuk is that possible.
Yes they did. Which is why I had to come up with this type of a solution which takes more time for calculation during every transaction.
2
u/sozesghost May 15 '18
A list of things not in any particular order.
You did not test many edge cases, like invalid transaction amounts (negative) or negative timestamps etc. Actually, no clue if transaction amounts can be negative, but it's worth finding out.
Your verify transaction method is also adding the transaction (that's a side effect). Separations of concerns and all that.
Your tests are using the same logic as the implementation your are testing, so if both are wrong, you will get a passing test.
Most of your logic is a DAO (which is in a repository package, shouldn't it be a repository?), but those objects concern themselves with saving/retrieving data, not business logic.
If you stored your data is a ordered Collection, you would not have to calculate min and max at all. See next point.
As for the get in contant time and memory, you are storing the transaction amounts in a list so they will grow. What you can do is update the relevant values (sum, avg, count, min, max) and not store the list at all. Storing the previous max and min seems cumbersome but seems to satisfy their requirements.
Your StatsDaoImpl is storing data in static variables. This does not help with concurrency, but keep in mind that Spring beans are Singletons by default, so you don't need the static modifier.
Your amountList variable is just a list, it's not synchronized so it's not thread safe. In fact, your addTransaction and expireTransaction methods are not thread-safe at all. Some operations in them are atomic, but the method call as a whole is not.
Any feedback (negative or positive) to my comments is appreciated, we can all learn something.
1
u/IamPilgrim May 15 '18
Thanks for the comment!
Your verify transaction method is also adding the transaction (that's a side effect). Separations of concerns and all that.
Frankly, I didn't think it mattered! Maybe I should have renamed it to
verifyAndAdd?Your tests are using the same logic as the implementation your are testing, so if both are wrong, you will get a passing test.
Can you expand a bit more on this? I have never written any tests before this.
If you stored your data is a ordered Collection, you would not have to calculate min and max at all
Also true. I could have done that but there could be a case where network latency from the client resulted in the transaction coming in at a considerably later time, and other transactions(with timestamps later than the previous transaction) could have arrived before it.
Spring beans are Singletons by default, so you don't need the static modifier.
TIL.
Your amountList variable is just a list, it's not synchronized so it's not thread safe. In fact, your addTransaction and expireTransaction methods are not thread-safe at all. Some operations in them are atomic, but the method call as a whole is not.
Hmm, this makes sense. I will keep this in mind the next time I want thread safe operations. TBH, I've learnt more about programming practices in this thread than I have learnt in my company in the last 6 months.
2
u/sozesghost May 15 '18
Frankly, I didn't think it mattered! Maybe I should have renamed it to verifyAndAdd?
I would not do that. I would have one method for verifying and one for adding. Readability and predictablity above all, unless you have a very good reason to combine them.
Can you expand a bit more on this? I have never written any tests before this.
I will try. This is your addTransaction method in StatsDaoImpl (empty lines omitted):
private void addTransaction(Double amount) { amountList.add(amount); count.incrementAndGet(); sum.addAndGet(amount); avg.set(sum.get() / count.get()); max = Collections.max(amountList); min = Collections.min(amountList); }And this is addTransaction in your tests:
private void addTransaction(double amount) { amountList.add(amount); count.incrementAndGet(); sum.addAndGet(amount); avg.set(sum.get() / count.get()); max = Collections.max(amountList); min = Collections.min(amountList); }You see, they are the same except for the type in the signature. What if you made a mistake and in one of them used:
min = Collections.max(amountList);and then copied that to the other? It would be wrong, but tests would agree with results. I mean, this is a small project so maybe it would not have mattered, but in general it's worth thinking about. Writing proper tests is not easy and writing bad tests probably even reduces value - they keep you feeling safe way too long. What can be done to fix it here? Use a package that is already tested and battle tested and will provide you with correct results. Or use windows calculator, pick your significant digits and test against that. Again, perhaps this does not matter for this project, but it's worth keeping in mind.
Also true. I could have done that but there could be a case where network latency from the client resulted in the transaction coming in at a considerably later time, and other transactions(with timestamps later than the previous transaction) could have arrived before it.
Don't think it would matter, even if they arrived 65 seconds late, it would be fine (they would be discarded), if they arrived before 60 seconds they would be inserted as normal. You can have a Collection that would insert the transaction at the appropriate place. Perhaps I'm misunderstanding your comment.
I will be honest, I don't understand others' comments about wrong HTTP codes. I'm not saying they are wrong or not, they seem alright to me, but would love to hear arguments why that's wrong. To me, 204 here is not a wrong input or a faulty response or an error, just that no content was created. It can happen, as you said there could be network congestion, the message was stuck in a queue somewhere etc.
1
u/abigreenlizard May 21 '18
Hey can I ask how you did that cool code formatting?
1
u/sozesghost May 21 '18
Blank line before code and indent each line with 4 spaces (more if you need deeper indents, but at least 4). Guide.
1
2
u/stonguse May 15 '18
Side effects in the verify method
The verify method adds the transaction. I would expect a verify method to only verify and do nothing else.
1
u/IamPilgrim May 15 '18
I answered this in another comment above
Frankly, I didn't think it mattered! Maybe I should have renamed it to
verifyAndAdd?
1
u/squishles May 15 '18 edited May 15 '18
most ides have a coverage report thingy built in, you can also run it through a coverage report build tool like sonar. It helps you spot edge cases you may have otherwise ignored, like branch conditions an if with one boolean needs 2 tests, an if with two booleans needs 4 tests, n2 ect.
It's not the greatest code, but they're still kind of picky here you show understanding of a lot of useful things, Your http response codes do make no sense though, couple other wtfs like boolean valid....; valid==true?....; Overall this'd fit nicely for a junior though, the derps can be beaten out in code reviews.
oo show a history of incremental commits too, when you don't it makes people suspicious you copy pasted it from somewhere or got your friend to write it for you.
1
u/IamPilgrim May 15 '18
Thanks for the info about the coverage report option. We at my org have never written any tests and consequently I don't know about it. We use a Sonar server externally though. I will use it the next time I test code.
Your http response codes do make no sense though.
Sadly, that's what they asked me to use.
22
u/nutrecht May 15 '18
I'm going to be a bit straight-forward / blunt. Sorry in advance, don't have much time ;)
Some feedback:
That just shows there's some really basic Java stuff you don't understand. You never need to call super() explicitly when you're just extending Object.
No need. Doing stuff you don't need to do shows you don't understand what you're doing. That's a bad habit that is a pretty red flag for devs.
Also why AtomicDoubles? You're not doing anything threading sensitive with these objects I hope?
Was this asked for? If not a 2xx response for a fault is a strange choice.
You have all your logic in the repository layer. A big no no; this should be in the service layer. Currently your services doesn't do anything at all.
To answer your questions:
Google "test coverage Java"? IntelliJ for example has this built in, and there's tons of tools / plugins that do this for you.
Your problem is much bigger. You somehow use a caching map you stuff something in but since it only is created locally in the method it gets garbage collected somewhere after this. So you create a complete map + expiration handler for each transaction and it doesn't even work properly. It really is a huge WTF.
You could easily have implemented this with a sortedset and throw away expired transactions every now and then.