Top 21 String Programming and Coding Interview Questions With Solutions

In this article, I am going to share 21 of the most common String-based Programming and Coding interview questions from Java developer interviews. These questions require you to write code to solve the problem and they are different from traditional Java String questions like how the substring method works in Java or when to use the intern() method of String in Java? Since coding and problem solving are an important part of any programming job interview, it's imperative that you know how to solve them in time and in a pressure situation, which comes only after doing practice with the right set of questions. Since these questions are already tried and tested and appeared in many interviews, they will provide you the experience you need to crack your coding interview.

A string is one of the most popular data structures, probably next to the array and that's why you will find at least one question from String in any programming job interview. If you don't know, String is nothing but a character array in most of the languages like C. But, in Java, String is an object, which holds data in a character array.

This means you can solve many String-based problems by assuming it as a character array. You can use all the tips and tricks you learned by answering an array-based coding question to also solve String, like reversing a String is the same as reversing an array and so on.

I have also provided the links to solutions for most of the questions so that you can quickly check your answer and code. Remember, it's not just essential to solve the problem but also to understand the solution and improve it gradually.

In a real interview, an interviewer is as much interested in the solution as in improvement and optimization. In order to improve the solution, good knowledge of data structure is mandatory because, in many cases, a choice of good data structure not only makes the solution simple but also fast. For example, by using a set, you can quickly figure out duplicate characters of String.

Also, you should be able to calculate the cost of your solution, like time and space complexity on Big O notation. If you haven't revise your data structure and algorithms, then Data Structures and Algorithms: Deep Dive Using Java is an excellent course to start with before you go for the interview.





String Programming and Coding Interview Questions

Here are 21 most frequently asked String-based questions from a programming job interview. These questions are not based on theoretical concepts, like Why String is Immutable in Java, but slightly based upon the string data structure and require a lot of coding practice to solve them in limited time.


1) How to find the maximum occurring character in a given String? (solution)
Write an efficient Java/C/Python program to return the maximum occurring character in the input string, e.g., if the input string is "Java" then the function should return 'a'.


2) How to remove all duplicates from a given string? (solution)
Write a program to remove all the duplicate characters from a given input String, like, if the given String is "Java" then the output should be "Java". The second or further occurrence of duplicates should be removed.


3) How to print the duplicate characters from the given String? (solution)
Write an efficient Java/C/Python/Ruby program to return the duplicate characters from given String, for example, if given String is "C++" then your program should print "+" Similarly, if the input is "Java and JavaScript" then your program should print "J", "a" and "v". You can ignore the case for finding duplicates.

If you notice there is a pattern to solve these duplicate characters problem. For example, you can use a data structure like HashSet or HashMap to store characters and their count to find out which is duplicate and how many times they are appearing. Knowing these coding tips and patterns really help during coding interviews. 

If you are interested to learn more about these kinds of coding patterns then I highly recommend you to checkout Grokking the Coding Interview: Patterns for Coding Questions course from Educative, an interactive learning portal. This course will teach you 15 common coding patterns like sliding window, two pointers, merge interval, etc, that you can use to solve 100+ Leetcode problems. 

21 String Coding Problems from Java Interviews




4) How to remove characters from the first String which are present in the second String? (solution)
Write an efficient Java/c/Python function that takes two strings as arguments and removes the characters from the first string, which are present in the second string. For example, if the first String "India is great" and the second String is "in" then the output should be "da s great"


5) How to check if two strings are rotations of each other? (solution)
Write an efficient program to test if two given String is a rotation of each other or not, e.g. if the given String is "XYZ" and "ZXY" then your function should return true, but if the input is "XYZ" and "YXZ" then return false.


6) How to reverse a given String? (solution)
Write a program to reverse a String in C/Java/Python or choice of your programming language. You can write either the recursive or iterative solution. For example, if a given input is "abcd," then your function should return "dcba".


7) How to reverse String without recursion? (solution)
Same as the previous program, but you cannot use recursion, this time, you must solve this problem in C/Java/Python using iteration or loops. If you are not familiar with recursion and repetition, you can further check the Algorithms and Data Structures - Part 1 and 2 courses on Pluralsight. One of the better course to start with fundamental techniques to solve algorithms based problems.

string programming interview questions for java developers



8) How to print all permutation of a String? (solution)
Write an efficient program to print all permutations of a given String in Java/C/Python or any programming language of your choice. For example, if given input is "123" then your program should print all 6 permutations e.g. "123", "132", "213", "231", "312" and "321".


9) How to find the first non-repeating character in a given String? (solution)
Write a program in any programming language you to find the first non-repeated character in a given String, for example, if the given String is "Java" then the first non-repeated character is "J"


10) How to reverse the words in a given String sentence? (solution)
Write an efficient program in Java or C++ to reverse the words in a given String sentence. For example, if the input is "Java is best," then your program should print "best is Java". There is no restriction on preserving white space.


11) How to find the smallest substring in a given string containing all characters of another string? (solution)
Write an efficient function in C/C++ or Java to print the smallest substring in a given string containing all characters of another String. For example, if the given String is "this is a test string" and "tist", then the output should be "t stri".


12) How to check if two given String is the anagram of each other? (solution)
Write an efficient program in Java/C/C++ to check if two String is an anagram of each other. An anagram contains are of the same length and contains the same character, but in a different order, for example, "Army" and "Mary" is the anagram. Your program should return true if both Strings are the anagram, false otherwise


13) How do you check if a given String is Palindrome or not? (solution)
Write an efficient program in your favorite programming language to check if a Given a string is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"121" is a palindrome, but "123" is not. Your function should return true if given String is a palindrome, false otherwise


14) How do you convert String to an integer?  (solution)
Implement atoi() like function in C/C++/Java to convert a string to an integer. Consider all possible cases e.g. positive and negative String, the presence of + or - character, etc. For example, if the given input String is "123" then your program should return 123 and if a given input is "+231" then your program should return 231

I first learned about this problem on Programming Interviews Exposed: Secrets to Landing Your Next Job, one of the finest books on interviews, and still its explanation is the best I have read so far.

String Programming Interview Questions With Solutions



15) How do you convert a Roman numeral String to Integer in Java? (solution)
Write a function in your favorite programming language to convert a given Roman numeral to equivalent String. For example, if the given String is "X" then your program should print 10. Input will be within the range from 1 to 2000. You can also ignore case, like both "x" and "X" should return 10


17) How do you remove a given character from String? (solution)
Write an efficient method in Java to remove all occurrences of a given character in Java. For example, if the given String is "Programming" and the given character to remove is "m" then your function should return "Prograing".


18) How do you count the number of words in String? (solution)
Write a program to count a number of words in a given String. The words are separated by the following characters: space (‘ ‘) or newline (‘\n’) or tab (‘\t’) or a combination of these. For example, if input "Java is great" your program should print 3.


19) How do you check if a given string contains valid parentheses? (solution)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', write a function in Java or C++ to check if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not


20) How do you find the longest palindromic substring of a given substring? (solution)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 

This problem can be solved using the Dynamic Programming technique. If you don't know what is Dynamic Programming I suggest you take the Dynamic Programming Java, Coding Interviews and Applications course on Udemy, an excellent course to begin with Dynamic Programming.



21) How to convert Integer to Roman String? (solution)
This is the reverse of problem 17, In this problem, you need to write a function in Java/C or C++ to convert a given integer into an equivalent Roman numeral. For example, if a given integer is 5, then your program should print "V".

Here is a chart of Roman numerals for 1 to 100 for your reference:

21 String Programming and Coding Interview Questions Answers


That's all about some frequently asked String Programming interview questions for preparing Programming Job interviews. The string is an important topic, so leave it at your peril. You might have seen this question before, but solving them again and understanding their performance will help you a lot in doing well in programming interviews.


Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Grokking the Coding Interview: Patterns for Coding Questions
Cracking the Coding Interview: 189 Programming Questions and Solutions


Other Data Structure and Algorithms Resources you may like
  • 75+ Coding Problems from Interviews (questions)
  • 10 Free Courses to learn Data Structure and Algorithms (courses)
  • 10 Books to Prepare Technical Programming/Coding Job Interviews (books)
  • 10 Courses to Prepare for Programming Job Interviews (courses)
  • 100+ Data Structure and Algorithms Interview Questions (questions)
  • My favorite Free Algorithms and Data Structure Courses on FreeCodeCamp (courses)
  • 30+ linked list interview questions with a solution (linked list)
  • 30+ array-based interview questions for programmers (array)
  • 10 Coding Tips and 101 Coding Questions for Interviews (tips)
  • 50+ Algorithm based Interview questions from HackerNoon (questions)
  • 5 Free Courses to learn Algorithms in-depth (courses)
  • 10 Algorithms books every Programmer should read (books)
  • Top 5 Data Structure and Algorithms Courses for Programmers (courses)

Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.


P. S. - If you are looking for a FREE course to learn Data Structure from scratch, you can also check out these free Data structure and algorithms courses from Udemy, Pluralsight, Coursera, Educative, and other popular online portals. If you are a beginner looking to find your feet on Data structure and Algorithms, I highly recommend you to join these free courses. 

10 comments:

  1. Hi guys,
    here the first question is "How to find the maximum occurring character in given String?"

    but the solution link is for "How to Count Occurrences of a Character in String"

    Both are different, Please confirm...

    ReplyDelete
    Replies
    1. Yes, you are right but once you know how to count character then the other one becomes simple because you just need to return the character with maximum count

      Delete
  2. yes both are different

    example

    1)How to find the maximum occurring character in given String?"

    Malyalaum - a-3 (a is the maximum occurring character in given string)

    2) How to Count Occurrences of a Character in String
    Malyalaum
    m-2
    a-3
    y-1
    u-1

    thank you,

    ReplyDelete
    Replies
    1. That's correct but once you know how to count character then the other one becomes simple because you just need to return the character with maximum count

      Delete
    2. public class OccObj {
      public static void main(String[] args){
      Scanner input=new Scanner(System.in);
      String s= input.next();
      NoOfOcuu(s);
      }
      public static void NoOfOcuu(String s){
      int c=0,c2=0;
      ArrayListn=new ArrayList<>();
      char c1='0';
      for(int i=0;i=i)&&(c2==0)) {
      if (s.charAt(i) == s.charAt(j)) {
      c++;
      }
      }
      }
      if(c>1){
      n.add(s.charAt(i));
      }
      if(c>0) {
      System.out.println("the vale:" + s.charAt(i) + " - " + c);
      }
      }
      }
      }

      Delete
  3. public static void NoOfOcuu(String s){
    int c=0,c2=0;
    ArrayListn=new ArrayList<>();
    char c1='0';
    for(int i=0;i=i)&&(c2==0)) {
    if (s.charAt(i) == s.charAt(j)) {
    c++;
    }
    }
    }
    if(c>1){
    n.add(s.charAt(i));
    }
    if(c>0) {
    System.out.println("the vale:" + s.charAt(i) + " - " + c);
    }
    }
    }

    ReplyDelete
  4. 19)function isPalindrome(str) {
    let emptyArr = [];

    for (let i = 0; i < str.length; i++) {
    let x = str[i];

    if (x == "(" || x == "[" || x == "{") {
    emptyArr.push(x);
    continue;
    }

    if (emptyArr.length == 0) return false;

    let y;
    switch (x) {
    case ")":
    y = emptyArr.pop();
    if (y == "{" || y == "[") return false;
    break;

    case "}":
    y = emptyArr.pop();
    if (y == "(" || y == "[") return false;
    break;

    case "]":
    y = emptyArr.pop();
    if (y == "(" || y == "{") return false;
    break;
    }
    }

    return emptyArr.length == 0;
    }

    if (isPalindrome("{}")) console.log(true);
    else console.log(false);

    ReplyDelete
  5. val str = "aabbbbaa"
    var l = str.length
    var s = ""
    while (l>0){
    s = s + str.get(l-1)
    l--
    }
    if(s.equals(str)) {
    println("palindrome string : " + s)
    }else{
    println("No palindrome string : " + s)
    }

    ReplyDelete
  6. Hi guys,
    here is the solution for question number 4 "How to remove characters from the first String which are present in the second String?"
    public static void main(String[] args) {
    String st1 = "india is great";
    String st2 = "in";
    List secondStringList = st2.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
    String outputString = st1.chars().mapToObj(c -> (char) c).filter(c -> !secondStringList.contains(c)).map(ch -> new String(String.valueOf(ch)))
    .collect(Collectors.joining(""));
    System.out.println("outputString : " + outputString);
    }

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.