loading...

Top 20 String Coding Problems from Programming Job Interviews

javinpaul profile image javinpaul Updated on ・8 min read

Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.


Along with array, binary tree, and linked list data structures, the string is another popular topic on programming job interviews. I have never participated in a coding interview where no string-based questions were asked.

This is very obvious because I have also never written a program where I have not used a single String variable. You will always find String as one of the most used data type or data structure in any program.

In this article, I am going to share some of the most common String based coding problems I have come across from many Programming interviews I have been part of. I also have experience from both side of the table as a candidate as well as Interviewer so I know how important these questions are.

Btw, there is no point solving these questions if you don't have basic knowledge of data structure or you have not to refresh them in recent time. In that case, I suggest you to first go through a good data structure and algorithm course or book to revise the concept.

This will save you a lot of time going back and forth between the book and your IDE for each question.

If you need resources, I suggest following online courses to learn Data structure and Algorithms, even though they are independent of any programming language, I strongly suggest you join the course which explains problems in the programming language you are most comfortable with.

Like

  1. Data Structures and Algorithms: Deep Dive Using Java for Java developers
  2. Algorithms and Data Structures in Python for those who love Python
  3. JavaScript Algorithms and Data Structures Masterclass by Colt Steele for JavaScript programmers
  4. Mastering Data Structures & Algorithms using C and C++ for those who are good at C/C++

How to solve String based Coding Problems?

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.

So all the techniques you know by solving array-based coding questions can be used to solve string programming questions as well.

Here is my list of some of the frequently asked string coding questions from programming job interviews:

  1. How do you reverse a given string in place? (solution)
  2. How do you print duplicate characters from a string? (solution)
  3. How do you check if two strings are anagrams of each other? (solution)
  4. How do you find all the permutations of a string? (solution)
  5. How can a given string be reversed using recursion? (solution)
  6. How do you check if a string contains only digits? (solution)
  7. How do you find duplicate characters in a given string? (solution)
  8. How do you count a number of vowels and consonants in a given string? (solution)
  9. How do you count the occurrence of a given character in a string? (solution)
  10. How do you print the first non-repeated character from a string? (solution)
  11. How do you convert a given String into int like the atoi()? (solution)
  12. How do you reverse words in a given sentence without using any library method? (solution)
  13. How do you check if two strings are a rotation of each other? (solution)
  14. How do you check if a given string is a palindrome? (solution)
  15. How do you find the length of the longest substring without repeating characters? (solution)
  16. Given string str, How do you find the longest palindromic substring in str? (solution)
  17. How to convert a byte array to String? (solution)
  18. how to remove the duplicate character from String? (solution)
  19. How to find the maximum occurring character in given String? (solution)
  20. How do you remove a given character from String? (solution)

These questions help improve your knowledge of string as a data structure.

If you can solve all these String questions without any help then you are in good shape.

For more advanced questions, I suggest you solve problems given in theAlgorithm Design Manual by Steven Skiena, a book with the toughest algorithm questions.

If you need to revise your Data Structure and Algorithms concepts then you can also see these resources:

1. Data Structures and Algorithms: Deep Dive Using Java for Java developers

2. Algorithms and Data Structures in Python for those who love Python

3. JavaScript Algorithms and Data Structures Masterclassby Colt Steele for JavaScript programmers

4. Mastering Data Structures & Algorithms using C and C++ for those who are good at C/C++

These are some of the best courses on data structures and algorithms and you can choose the one which is most suitable for you. Btw, I will receive payments if you buy these courses

Now You're Ready for the Coding Interview

These are some of the most common questions outside of data structure and algorithms that help you to do really well in your interview.

I have also shared a lot of these questions on my javarevisited and java67, so if you are really interested, you can always go there and search for them.

These common String based questions are the ones you need to know to successfully interview with any company, big or small, for any level of programming job.

If you are looking for a programming or software development job in 2018, you can start your preparation with this list of coding questions but you need to prepare other topics as well.

This list of 50+ data structure and algorithms problems provides good topics to prepare and also helps assess your preparation to find out your areas of strength and weakness.

Good knowledge of data structure and algorithms is important for success in coding interviews and that's where you should focus most of your attention.

Further Learning
10 Algorithm Books Every Programmer Should Read
Top 5 Data Structure and Algorithm Books for Java Developers
From 0 to 1: Data Structures & Algorithms in Java
Data Structure and Algorithms Analysis --- Job Interview
50+ Data Structure and Coding Problems for Programmers
10 Data Structure, Algorithms and SQL Courses to Crack Coding Interviews

Closing Notes

Thanks, You made it to the end of the article ... Good luck with your programming interview! It's certainly not going to be easy, but you are one step closer after practicing these questions.

If you like this article, then please share with your friends and colleagues, and don't forget to follow javinpaul on Twitter!

P.S. --- If you need some FREE resources for Programming Job Interviews, you can check out this list of free data structure and algorithm courses to start your preparation.

Posted on Jun 1 '19 by:

javinpaul profile

javinpaul

@javinpaul

Java Programmer and blogger

Discussion

markdown guide
 

"Thank you for selecting me as a candidate to interview, if you honestly think I've worked my way up to being a senior developer without knowing how to actually perform the job, then this isn't an organization I want to work for. I'm sure we both have more important things to do with our time, so why don't we go ahead and end this now."

 

Well @Bradley, I do understand that after a certain level of experience you may find it offending solving these questions but that's not the goal. If you are hands-on with coding, you can easily solve these problems but if you struggle with solving these, it means you are a bit rusty and not really doing coding everyday.

 

It doesn't matter to me how easy the questions are to answer. To me:

The question is trying to verify that I know how to do trivial and/or esoteric operations off the top of my head, which lets be honest, who has ever had to reverse the words of a sentence? Likewise, who hasn't had to strip bad characters out of a string?

The interviewers care about my rote memorization, which has nothing to do with being a good developer.

The company put someone in charge of interviewing that isn't a developer and they pulled questions off the internet thinking this is what makes a great developer.

The person in charge of hiring is too busy or too lazy to put the time into determining if a long list of candidates are a good fit for their organization, so they use this as a quick way to weed out candidates in round one. Mission successful; if they're not going to put the time into selecting quality candidates then right off the bat I'm not too excited about my prospective coworkers, nor the support I can expect from the organization.

What I'm saying is that a company asking this type of question tells me all I need to know about that company.

About the most I'll do is some sort of take home project and discuss at the interview: at least then I can either use a language or library I haven't used in a long time or learn a new one while I'm doing the project so that I'm not wasting my time.

Exactly! Job interviews are a two-way thing. Or at least they should be.

 

Horrible idea for a job interview. Perhaps for a junior position, or even for an internship, but not for a senior, or at least for minimally independent position. All of those problems are easily solvable with Google.
On the other hand, as a senior, you have to work with multiple different languages, technologies, paradigms and be able to to figure out best way to solve the problem, having multiple proposed solutions. Problems you referred are trivial, well known and deeply exploited.

From my experience, I had people who excelled at this kind of questions but failed at "as a java developer i won't touch js/html" kind of test. As a scrum master and main developer on the team I cannot accept situation where one of the members can't (don't want to) handle tasks other people do.

As interviewer I like this question: which features of java8+ are most important to you?
It gives the interviewee chance to gather thoughts, check if he is up to date and on which parts of the language he focuses. And there is no "bad" answer. All answers reflect how they think, what they focus on, and, honestly, if you rate people's creativity by number of algorithms they memorized, you'll assemble a team of losers who can't solve any problem unless it's already solved by a business analyst.

I don't want to work for you. Sorry.

 

The java example of reversing a string in-place is wrong. Strings are immutable in Java. The example given will involve two array copies.

 

Well, while you are correct, it's about logic. In reality, it's better to use StringBuilder.reverse() but yes, mentioning that fact can impress some interviewers too.

 

Possibly the worst way to hire someone. They give zero indication of a devs ability.

 

I don't know why you think so because this is not the only interview anyone will give to get the job. It's not that easy. this is just a part of a big hiring process.

 

I think that trying to let things clear during the interview helps the candidate to understand the importance behind these kind of questions. In one of the process I participated the interviewer explained that solving the string problems I was facing was not the point. He explained the time constraints the projects used to demand in the company and for that reason code performance and complexity concerns would be also evaluated. String problems themselves probably do not represent the scenario a candidate will be working on, but perhaps telling about the skills required to not hit the iceberg helps to care about its top.

 

For comparison, look here:
businessinsider.com/how-tech-compa...

If paywalled, disable javascript.

 

Ive always argued against this type of interview. Its pretty frustrating because developers that knows how to solve these problems dont actively enhance the business or product they apply for. The ones that would fail this exam in the interview could learn how to solve these problems on the job yet make great contributions to the domain. Interviews should ask domain driven questions because that's what drives the product and the business.. away from this mentality. 14 years as a programmer.. and never once was asked how to figure out a palindrome. What difference does that make? And if it does.. why can't I figure it out after I get hired?

 

Hello Sultan, there is a definite value of asking this question, it tells about their coding skills, problem-solving ability, how to convert thoughts and logic to the code, testing, analysis and a lot more which is key skills to work as a software developer. That's the reason these kinds of coding interview questions have survived the test of time.

 

Hi Javinpaul,

But thats why developers get certification and degrees and soending a lot of time and $$$ to do it. When you hire a roof contractor do you let them test if they can place shingles properly? Or when you choose a dentist, do you let them test on model teeth before they test yours? Its a shame that the dev community can't come up with better ways to trust devs and test their knowledge on domain drive design, network systems, project configurations, and all the existentials that comprise a project. These types of question are good college exams.. but I just dont see how they are that great for interviews.. its a bit archaic. But it is what it is. I really like your article btw, and don't mean to pick at it like that. Id actually use your samples and tests when I teach programming to my students.

Hello Sultan,
Thanks for your appreciation, yes, you can feel free to use the article for educational purpose. Regarding your question, I agree with you but the irony is that there is a big gap between the interview and the actual work you do. That's why only general skills are tested like learning ability, problem-solving etc. Now companies are moving towards more stringent online coding test where you need to code a scenario based program but still that's far away from what you will do in the actual job.

 

The idea is to see how you go about solving problems. The actual problems themselves aren't really all that important. One question I have been asked is "How would you calculate the number of seagulls in the city?". At first glance, this question has nothing to do with software engineering and it certainly isn't a programming problem.

 

But that's experimental. Anyone can solve those types of problems in their own setting. Interviews contains very limited resources, so it makes simple problem solving more difficult than needed and completely unrelated to what your job responsibilities are. Why do interviews have this? Simple.. laziness. I been asked to define "virtual" in almost every interview.. and to do an algorithm to sort a list.. taking the same boilerplate interview template. To be even more honest, usually job responsibilities are in fact way easier than questions asked during the interview. Better off making these questions as take home, and have the person discuss the solution during the interview. Cheating in take home is fine.. whats important is the interview.

I can relate to that. I was asked in an interview what "type erasure" was. My answer: "I can't remember". They still offered me the job, so what was the point of that question?