r/compsci • u/javinpaul • May 17 '19
20+ String Coding Interview Questions for Programmers
https://hackernoon.com/20-string-coding-interview-questions-for-programmers-6b6735b6d31c26
u/Nerdlinger May 17 '19
A good thing about the string data structure is that if you know the array data structure, you can easily solve string-based problems because strings are nothing but a character array.
Enh. This is really language dependent. For example, it is not true in Swift, and as a result, some of your answers (like for string reversal) are poor answers.
0
May 17 '19
[deleted]
7
u/Nerdlinger May 17 '19 edited May 17 '19
No, I’m talking about the fact that they aren't Arrays. They are both Collections, but Strings are not Arrays. Their relationship is more like humans and orangutans than humans and apes.
This does bring in to play things like non-constant time random access, but it also brings into play the various features of the protocols that Strings conform to that make solutions like index/position swapping for reversal a poor choice for the bulk of use cases.
0
19
u/icendoan May 17 '19
strings are an array of characters
Not in utf8, they're not. Ascii is not the be all and end all.
5
u/quentech May 17 '19
What runtimes store strings in memory as utf8?
3
u/nikeinikei May 17 '19
I think Rust, but I may be wrong
1
u/masklinn May 18 '19
You’re correct, a rust string is stored as utf8. In fact, non-utf8 str is a specific example of the « Invalid values in primitive types » UB.
3
u/czdl May 17 '19 edited May 17 '19
Cocoa / OSX
Windows uses UTF16 which has exactly the same issue.
1
u/Nerdlinger May 17 '19
I believe Objective C/NSString still uses UTF-16 for the backing store, though Swift recently switched to UTF-8.
1
u/czdl May 18 '19
Perhaps on iOS, but querying NSString for its representation, or pulling bytes out on OSX reveals UTF-8.
0
u/quentech May 17 '19
has exactly the same issue
Windows uses fixed 16-bit width characters and string are in fact arrays of them.
1
u/nzodd May 18 '19
What do you mean, the win32 api? So how does it store characters outside the BMP then ?
1
u/masklinn May 18 '19
Same as java or JavaScript, surrogates are visible and you can easily get or create outright invalid text, even ignoring the grapheme cluster issue.
0
u/czdl May 18 '19
They’re arrays of them in the same sense that utf8 is an array of characters. If you don’t handle surrogates, you break the string.
1
May 17 '19
Strings are unicode in many languages but probably aren't encoded with utf-8 all the time.
1
u/Keegbear May 17 '19
interesting, how does utf8 work in this manner?
2
1
u/masklinn May 18 '19
The normal way? The backing buffer is an array of code units (bytes), and string-level APIs either decode to codepoints or grapheme clusters on the fly (for iteration or classification) or operate directly on the backing buffers (no need to decode to concatenate two strings, you can just concatenate the bytes).
It does mean you don't get straightforward O(1) indexing for instance. Though that's mostly a good thing as unicode complexities means O(1) access to codepoints (or code units) is mostly useful to fuck up.
15
u/chrism239 May 17 '19
I'm wondering how many programmers, working on a 'real' project, have ever needed to reverse the characters in a string, or the words in a sentence, or needed to check for palindromes? Just wondering.
6
u/Scottishboy614 May 17 '19
I have! In order to use a positive lookahead regex, which was (maybe still is?) Unsupported on Firefox, I had to reverse a string and make backwards regex back & forth to take advantage of positive look behind. It was as messy as it sounds
6
4
u/icendoan May 17 '19
Sure. It's not a bad question because you're not expected to do exactly that; the idea is that you can apply algorithmic thinking to a problem. If it's an interview, you want to do it quickly, and not spend hours hashing out all the real world edge cases.
12
May 17 '19
[removed] — view removed comment
1
May 18 '19
My current company asked me to implement a binary parser from a set of requirements in the comfort of my own home. That's really when I knew I wanted to work there.
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
1
u/Kinglink May 18 '19
Please walk out of these interviews.
If a company wants to pay me exceedingly well and I have to hop through a few memorization tricks great. Even better if I get the answer through my simple understanding of Data structures, algorithms, and being able to demonstrate an ability to code.
Every time someone posts one of these things there's a guy who goes "Well these questions are stupid and pointless." Which instantly tells me two things. A. I don't want to work with that person. B. That person is an interviewee, never an interviewer.
Bad candidates get through pre-screening. They can cheat, just not be asked the right question or somehow fool the HR department.
But questions like this actually test more than "rote learning and minutiae" and if you answer them and pay attenton to what's ask you're probably start noticing it.
A candidate needs to prove he can code, not just in a controlled enviroment, or even at home, but in front of someone else. Again cheating is a big thing, and when you're offering a good salary for a job, there's incentive to cheat, even for a short term job. Every interviewer has had an interview where the candidate didn't know how to code.
But beyond that, every question here is fucking easy. I don't mean "Well I know the answer." I mean "if you can't answer this, you're probably not a good programmer", which is why study guides like this is eye rolling.
There are definitely better answers, and worse answers, but if you need "Rote memorization" for these questions.. that's a really bad sign, and a great reason not to hire the person.
Beyond that though, I've worked with enough asshole who will say something like "That's beneath me" or "you don't have to do it that way" and you know what I really don't want another. If questions like this get those people to walk out or show their cards in the interview process, I'm glad I ask them and will keep on doing so. .
3
u/pythonaut May 17 '19 edited May 17 '19
Honestly, I would rather a new developer learn about regex and character encoding than master these string riddles. "Oh, but I need an algorithm to determine whether this string is an anagram of this other string!" said no one.
104
u/[deleted] May 17 '19
If interviews ask you bullshit string questions like this don’t work there. Stop propagating a culture of rote learning and minutiae worship. This is why our industry sucks and most code in the wild is abysmal.