5

I'm a beginner learning Java and after reading the docs I'm trying to solve some of the problems at codingbat.com. Some I managed to solve pretty quickly in a matter of seconds but some not quite. The problems themselves are easy (example http://codingbat.com/prob/p126212) but I sometimes have a hard time finding the solutions. In part because I think of the best possible solution and automatically discard the ones that might work but aren't so elegant and in part because I can't find the right algorithm. This is either due to my inexperience (I have used PHP for web development for about 2 months so I'm not a total beginner - even worked with one framework to build a website) or due to my inability to think of a good algorithm.

I normally don't look at the solutions. I sometimes get stuck even for 1 1/5 hours at simple problems and some of them I eventually solve but some I can't so I eventually look at the solution.

Is it normal/good/bad to concentrate this long if I find a problem I can't solve? I might overreact but should this be so hard for me? Is there such thing as not being able to think of algorithms even with much learning and reading?

Thank you all in advance.

6
  • I'm not sure what's so hard about return str.replace("yak", "");. Can you point to a more difficult problem?
    – Josh K
    Nov 23, 2010 at 18:01
  • That's what I did for that one too... I checked the solution as well... and it's terrible. At the very least they should have used a StringBuilder. I guess these are for beginners, but it would be nice to show something with best practices. Nov 23, 2010 at 21:46
  • 4
    You are a complete beginner. You will realize that you have only just begun to learn after you have been programming for 10 years. :D Nov 23, 2010 at 21:48
  • 1
    The solution at that site may not be optimal - but it does solve the problem, whereas @Josh's answer does not. The actual problem says, "return a version where all the yak are removed, but the a can be any char" - implying that a is a meta-character, and strings such as yek would also be removed.
    – Cyclops
    Apr 26, 2011 at 17:40
  • 1
    True, a regex is the simplest solution. I guess it depends on the purpose of the code - I was sort of thinking the question was similar to those interview questions, where they say, "write a string copy, without using the built-in string library", and expect you to code a loop or something. And their answer would be translatable into any language, even if it didn't have a regex library.
    – Cyclops
    Apr 26, 2011 at 18:00

5 Answers 5

8

Its actually a pretty easy situation to get into when you're coding for yourself. Its bad to spend a long time focusing on the best way to do something when you don't produce anything in the end.

However I think it's also bad to never stop and reflect. You are pretty much a beginner with only a few months dev. experience if I read your question right. At this point in time you should be focusing on writing something, anything. If you're writing for yourself. If you're not writing for yourself you'll learn to be quicker as if you don't your job will be in trouble.

My advice is

  1. Write code. Bad code / good code it doesn't matter at this stage. You just need to know you can bash something out when required.
  2. Set yourself deadlines. Try to be reasonable with them but not so loose as you may as well not have any deadlines.
  3. Don't try to alter functioning code to be better written unless.
    1. You have a performance issue you need to fix.
    2. You have enough time and you feel that your new way is an awful lot better.
    3. You feel you can learn something new by trying to implement in a different way.
  4. Pay attention to the small details, such as comments. It can really help you fix your mind on the problems and the solutions with your own code when you comment it. Try it, it allows you to step away from the solution and focus on the problem.
  5. Get other people to review your work if possible.
  6. If doing this as a hobby enjoy it. If the worrying about the problems is taking away from your enjoyment, it implies your doing something wrong.
  7. Ask people what they would do with the same problems. This may be the best site for that.

Code that functions but is written badly is a lot more useful than no code.

One last comment that may help you. The interface to your code is a lot more important than the code itself.

Finally In time you will gain speed and confidence. If you can't look back at your work in a years time and say "What was I thinking?" then you really haven't learn't enough.

2
  • 2
    "Code that functions but is written badly is a lot more useful than no code." How true.
    – HLGEM
    Nov 23, 2010 at 22:07
  • "The interface to your code is a lot more important than the code itself." +1
    – Seralize
    Apr 11, 2012 at 16:55
9

From what you've said in your question, the issue isn't that you can't solve the problems, it's that you can't think of an elegant solution that would work, so you give up. The main problem with this approach is that you're not really learning anything.

A better approach would be to try one of the not so elegant possible solutions. If it doesn't work, it's not the end of the world, just move on to plan B. If it does work, you now have the chance to make it more elegant. Either way, you will have learned something, and this approach will serve you better if and when you write code for a living. Remember: you can't improve what's not there.

2
  • 1
    @Robert: thanks for the edit. It was a paragraph fail on my part, which actually proves my point. Nov 23, 2010 at 18:11
  • That's precisely what I've learned to do. Come up with an inelegant solution to part of the problem, and keep nibbling at it. Nov 23, 2010 at 22:36
7

I don't really like solving hard problems. They are complicated and confusing and easy to screw things up. So I break them up into small problems then solve those smaller problems. It works pretty well.

4

Take a break every now and then. It's good that you aren't looking at the solutions right off the bat, but if you are well and truly stuck go ahead. You will still learn even if you did not find the solution yourself.

I've heard that it takes 10 years to master a skill (don't have a source for that, sorry). So, don't worry if it seems to take you a long time at first. By the way, 1.5 hours is not a long time to be figuring out a problem. If you continue in programming you will find problems that may take you days, even weeks to figure out. Keep on driving at the problems, and you will find as the months go by that it becomes easier and easier.

As far as "Is there such thing as not being able to think of algorithms even with much learning and reading?" is concerned, practice makes perfect. There are only a limited number of basic data structures, and the algorithms for working with them are the same for any implementation. Once you learn, really learn to apply a few algorithms, you will remember them and be able to use the knowledge in other problems. Over time this knowledge base builds up to the point where problems appear to all be variations on a theme rather than disconnected.

3
  • 1
    "10 years to master a skill" source: norvig.com/21-days.html Nov 23, 2010 at 21:23
  • Heck I've spent weeks trying to figure out a complex solution!
    – HLGEM
    Nov 23, 2010 at 22:06
  • +1 for the "variations on a theme rather than disconnected" observation
    – Gary
    Nov 24, 2010 at 11:49
3

The most important thing in software development is working software. This is especially so in commercial development.

A lot of people spend far too much time trying to be clever or come up with a supposed "elegant" solution when what they should be doing is producing working code.

What you should be doing is solving the problem as quickly as possible. Once you have proven your solution works, then is the time to refactor to make it elegant, fast, etc, if required. Often, once you have working code in front of you it becomes easier to see the more elegant solution.

1
  • This is certainly something I have been learning the hard way. I keep thinking I have to make everything Abstract right from the get-go. Nov 23, 2010 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.