• 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

Get the number of Vowels in a String without any String API

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was asked in an interview to write a program which prints the number of Vowels in a String. I started writing the code and used the charAt method of String when the interviewer asked me not to use any of the String API's.
And the String is just a word like "Hello" without any separators.

I couldn't answer the question. So I thought if any of you have an idea on how to do this.

Thanks !
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find interview questions like this annoying. I don't see the point of putting that kind of restriction. If the point is trying to see how "clever" of a coder you are, then I'd rather not hire someone who won't use available conveniences. I say be glad you're not hired by this guy if that's the way they work at that company. They'll only turn you into a bad programmer and there are already plenty of those to last a lifetime and more.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create a char array and assign the string to it. Iterate over the char array and check for vowels.

But what is the purpose of answering this question? I hate such interview questions!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Interviewer wrote: write a program which prints the number of Vowels in a String....not to use any of the String API's.


Correct answer wrote:Why?

 
Anand Athinarayanan
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Junilu Lacar - Exactly !

@Joe Harry - Thanks. Never Struck me at that moment but again it would have been no use anyway. No body is going to do that in any kind of job.

@ Maneesh -Wish , I could have answered like that
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably the answer is Regex.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anand Athinarayanan wrote:I started writing the code and used the charAt method of String when the interviewer asked me not to use any of the String API's...


Then my answer would have been: "Impossible", because even if they wanted you to use some other form (eg, char[]), if the text is in String form to start with, you'd need to use some part of the String API to convert it. [*]

Not only is the question awful; it sounds like they only told you about these moronic restrictions AFTER you started writing a (presumably correct) solution.

Way to go, interviewer. That's how we always hand out specs in the real world.

Winston

[*] Actually, not true. As was already mentioned, you could pass it to an I/O class or Scanner; but sheesh...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This particular example seems a bit silly, but I think there's nothing wrong with interview questions that specify not to use particular approaches (which may even be the best approaches) in order to see how the applicant would arrive at a different solution. We do not have enough information about the situation to arrive at some of the conclusions posted above, IMO.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I submit that there are better ways to see if a candidate can think out of the box and find workarounds to constraints. Barring the use of the String API for something where it's obviously the best way to go is not a good example of this line of questioning.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:but I think there's nothing wrong with interview questions that specify not to use particular approaches...


But not AFTER somebody's started coding. That's shifting the goalposts.

There's been a similar post recently from a course where someone was asked to trawl a String using recursion (and no loops).
???

I'm with Junilu. If you want to test someone's ability to think "outside the box" or test their knowledge of different approaches, be honest about it:

"Write a program that processes a Java String, character by character, without using any of its methods. Print out each character as proof."

Winston
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems like splitting hairs. The interviewee can't have spent have much time on this when getting to this point. And the interviewer knows that the goal post changed, and can take it into account - no harm done.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:That seems like splitting hairs...


Hunh?

I fear we're not going to agree on this one (and I fear also that we're getting off-topic), but if someone asked me to do something and then, AFTER I started working said:
"Oh, sorry. I want you to do it for 10 cents; not a dollar", or "...only with a hammer and chisel", I suspect I'd be tempted to poke out their eye with my screwdriver.

Winston
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, we are getting off topic. Although I suspect that from Anand's perspective there is no good way to stay on topic.

There's no need to agree - reasonable people can well disagree on this one. But questions similar to this one are asked in interviews, so while any one person may think that a bad approach, fact is it happens, and not all that infrequently. So to the extent that there are inexperienced folks reading this, my point is: you have to know what you're doing if solely based on an experience like this you were to decide not to work for a company, because you'd be opting out of good places as well bad ones.
 
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a simple solution:



In this case, it returns 13
 
Michael D Sims
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one compiles quite nicely on the command line. All you do after compile is type:

java CountVowels <string to count vowels in>

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:but if someone asked me to do something and then, AFTER I started working said:
"Oh, sorry. I want you to do it for 10 cents; not a dollar", or "...only with a hammer and chisel", I suspect I'd be tempted to poke out their eye with my screwdriver.



In my opinion, this may be a good thing during an interview. The purpose is to show your thinking process -- so you should be talking through what you are thinking while you are doing it.

And if the rules change while you are doing it, it will show your thinking regarding that too. How you can quickly adapt to constantly changing requirements. Furthermore, as you are changing, this will be a chance to show your arsenal of options. For example, you may have been talking about different options of using the String class, and this change in requirement will show that you also know regular expressions, etc.

Henry
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:In my opinion, this may be a good thing during an interview. The purpose is to show your thinking process -- so you should be talking through what you are thinking while you are doing it...


Yup, I can see that; so maybe it's just down to delivery. If someone allowed me to show what I can do, and then said "OK, now how would you tackle this if you had to use [reflection/recursion/streams/whatever]?" or "...do it without any loops?".

But don't start me working and then say "No, that's wrong. You're not allowed to use any loops", because at that point I'm likely to throw my toys out of the pram and reply: "Well, why didn't you say so at the %@#!& start you idiot.".

Winston
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:"Well, why didn't you say so at the %@#!& start you idiot."


Interview terminated.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Interview terminated.


Job never wanted.

Winston
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:

Winston Gutkowski wrote:"Well, why didn't you say so at the %@#!& start you idiot."


Interview terminated.



An important point to remember when being interviewed for a job is that you are also interviewing the interviewers. I did contract work for many years and three times cut interviews short when I thought that the interviewers were asking trivial and/or ridiculous questions that were not even 101 standard and did not test anything except my tolerance. One guy was so p*ssed off with me that rather than just escort me out of the building he had me escorted off the premises flanked by 2 security guards!
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:

Tim Cooke wrote:

Winston Gutkowski wrote:"Well, why didn't you say so at the %@#!& start you idiot."


Interview terminated.



An important point to remember when being interviewed for a job is that you are also interviewing the interviewers. I did contract work for many years and three times cut interviews short when I thought that the interviewers were asking trivial and/or ridiculous questions that were not even 101 standard and did not test anything except my tolerance. One guy was so p*ssed off with me that rather than just escort me out of the building he had me escorted off the premises flanked by 2 security guards!



I've been waiting years for an event like that and it's not as if I'm all that tolerant as any interview I have invariably involves a 2 hour drive.
 
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

Michael D Sims wrote:
java CountVowels <string to count vowels in>


Hmmm... the Force is strong with this one...

Very clever with the regex solution. But can you make it handle "and sometimes Y and very rarely, W" ?
 
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

Dave Tolls wrote:

Richard Tookey wrote:An important point to remember when being interviewed for a job is that you are also interviewing the interviewers. I did contract work for many years and three times cut interviews short when I thought that the interviewers were asking trivial and/or ridiculous questions that were not even 101 standard and did not test anything except my tolerance. One guy was so p*ssed off with me that rather than just escort me out of the building he had me escorted off the premises flanked by 2 security guards!


I've been waiting years for an event like that and it's not as if I'm all that tolerant as any interview I have invariably involves a 2 hour drive.


I might pay off a recruiter to get me an interview like that just before I retire. It'd be a great story to tell my grandkids, if I ever get that chance. But then again, I don't know if I'll ever retire. I hope that when I finally log out, it'll be with both hands on the keyboard!
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
I might pay off a recruiter to get me an interview like that just before I retire. It'd be a great story to tell my grandkids, if I ever get that chance. But then again, I don't know if I'll ever retire. I hope that when I finally log out, it'll be with both hands on the keyboard!



I retired about 8 years ago. Best work related thing I ever did because I can now do the sort of work I want to do and not have to design and implement the rubbish specified by the systems analysts and ignorant managers. Retiring has allowed me to return to the electronics I abandoned a couple of years after graduating and I can now play much more Bridge. I do more exercise than I found time to do when I was working and have lost 35 lbs. I am currently working on clearing my bucket list but I am likely to get my telegram from the queen before the list is empty.
 
Michael D Sims
Ranch Hand
Posts: 167
1
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Hmmm... the Force is strong with this one...


... and for my next trick, I will attempt to juggle three fully extended light sabers! (applause)

Junilu Lacar wrote:Very clever with the regex solution. But can you make it handle "and sometimes Y and very rarely, W" ?


You mean like this?



First run gave me:

The number of vowels is: 72
We seem to have had somewhere around 3 y's
and we very rarely counted w's, but we did count 3 of them.


Second run gave me:

The number of vowels is: 72
We seem to have had somewhere around 4 y's
and we very rarely counted w's, but we did count 1 of them.


etc. etc.

Who else writes comedy in object oriented programming? I ask you ... WHO?

 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote: But then again, I don't know if I'll ever retire. I hope that when I finally log out, it'll be with both hands on the keyboard!



You do realise that means you'll have the ultimate keyboard face as you lie there in the coffin?

Michael D Sims wrote:
You mean like this?



You could skip the random numbers and just go for an "Are you Welsh?" flag...
 
Marshal
Posts: 28193
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
That would be



for Welsh.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:That would be



for Welsh.



But is 'y' always a vowel in Welsh or does it, as in English, only act like a vowel sometimes?
 
Paul Clapham
Marshal
Posts: 28193
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
Actually Welsh is only part of the problem. The application hasn't been properly internationalized at all. It doesn't even consider all of the accented vowels used in European languages (I'd guess there's about 30 of them) let alone the vowels in other scripts like Greek and Cyrillic.

(And it's the W that is frequently used as a vowel in Welsh, as in words like "cwm" and place names like "Eglwyswrw".)
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to go hardcore linguist over this, vowels are sounds. The things you folks are counting are "vowel letters." So unless you're coding against audio input, y'all are just wrong to begin with.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:
But is 'y' always a vowel in Welsh or does it, as in English, only act like a vowel sometimes?



It's always a vowel.
Either an 'uh' or an 'ee'.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So year contains 3 vowels?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:So year contains 3 vowels?


Only if 'year' is a Welsh word
 
Anand Athinarayanan
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It feels great to read through all the replies in this post.

@ Michael D Sims - Using Regex came to my mind after I came out of the interview room.

Update - I didn't get the job but I did learn quite a lot from this thread and I'm happy to realize i have a lot to learn and a long way to go. Thank you everyone for your thoughts !
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for updating the thread, glad to hear you found the answers useful and good luck with your search for a job.
 
reply
    Bookmark Topic Watch Topic
  • New Topic