• Post Reply Bookmark Topic Watch Topic
  • New Topic

an interview question to count occurence of characters  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i saw this question on some website, i tried solving it , but i just made a mess out of it.. here is the question :


now , this is what i tried



i saw a code online, but i tried to do the same thing in my way, to understand what exactly happens. i think logically it shd be correct, but it is just giving answer in a wierd way. also, there was a part to find the longest chain of occurence of a character, how can we solve that?

thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You need to figure out exactly what the steps are to do this "by hand", that is, without using Java or any other programming language. you should then write those steps down--basic, precise, simple steps--in English (or whatever your native language is) or pseudocode or a combination thereof. Once you're confident they are accurate, simple, and precise, you go about translating them to Java. If any one of those steps doesn't translate easily to a simple Java construct, that step is probably too complex and needs to be broken down into smaller steps, or is not clearly specified and needs to be rewritten more clearly and precisely.

2. Since you already have code and it's apparently not doing what you want, you need to do what the pros do when they debug--add println() statements or use a debugger, so that you can see what's happening at each step of the way and compare that with what you think should be happening based on running through the steps with pencil and paper.

3. If you still can't figure it out and need to post a question here, you need to TellTheDetails.(⇐click) Just saying it's "giving answer in a wierd way" is a case of ItDoesntWorkIsUseless.(⇐click)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never write a single line of code until you know how you are going to solve the problem. Start by writing things down. And really mean writing it out, not just thinking about it in your head. putting it on paper (or in a document) forces you to think through all the steps. If you just do it mentally, you tend to skip/gloss over parts that later come back to haunt you.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:If you just do it mentally, you tend to skip/gloss over parts that later come back to haunt you.


Indeed. Even speaking the problem out loud, as if you're explaining it to someone to ask for his help, can bring out things you glossed over mentally. Writing it down is, of course, even better.

@OP: In fact, if you had collected enough data to explain in detail to the forum what problem you're having and what output you're observing, you may very well have suddenly realized how to fix it all on your own.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All these previous posters are correct.

My advice:

Start backwards!

What is the deliverable, in this case you need to display the starting position and count for the letter that occurs the most.
So, there are at least 3 variables here; letter,index,count.

How would you maintain that(those) while iterating through the input string?
What would you do if two letters have the same count? (Ask this? ;))
Would you just keep the highest count so far, or all and then find the result?

WP
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!