Friday, 19 July 2019

String interview Questions Part - 1

Hi Friends,

In this post, I'm going to share String interview questions that have been asked in multiple interviews.

Question 1:

How to print duplicate characters from String?

Answer 1:

Steps:
  • Run for loop on each character and add that character in HashSet.
  • If add() method returns false, just print that character[duplicate one].

Question 2:

Check if two strings are anagrams of each other?

Answer 2:

Two strings are anagrams of each other, if they contain same exact characters.
e.g.: army and mary are anagrams of each other. But Army and Mary are not.

Remember: String.contains(charsequence) checks for capitalization. So, if String is "Abc", then "Abc".contain("a") returns false.


Steps:
  • Compare lengths of both strings. If they are different, strings are not anagrams of each other.
  • Run for loop on 1st string and check for every character that whether it is present in another string.
  • And then create a new string from 2nd string by removing matched character.

Question 3:

Write the code to reverse a string using recursion.

Answer 3:

String sz = "abc";
int index = 0;
StringBuffer strBuffer = new StringBuffer();

public void main(String[] args){
    String strReversed = reverse(sz);
    System.out.println("Reversed String is : "+strReversed);
}

public String reverse(String str){
    index++;
    strBuffer.append(str.charAt(str.length()-index));
    if(index != str.length())
        reverse(str);

    return strBuffer.toString();
}



Question 4:

How will you check if a String contains only digits?

Answer 4:

Use Pattern class.

Pattern pattern = Pattern.compile(".*\\d.*");
boolean matched = pattern.matcher(inputString).matches(); // If it returns true, that means, String contains digits only.

Note: If we take Pattern pattern = Pattern.compile(".*\\D.*");, then matches() method will return false, if String contains all digits.


Question 5:

Find all permutations of a string.

Answer 5:





public class StringPermutations{
    public static void main(String[] args){
        String sz = "ABC";
        int length = sz.length();
        StringPermutation  perm = new StringPermutation();
        perm.permute(sz, 0, length-1);
    }

    private void permute(String sz, int len, int right){
        if(len == right)
            System.out.println(sz);
        else{
            for(int i = 1; i<=right; i++){
                sz = swap(sz, len, i);
                permute(sz, len+1, right);
                sz = swap(sz, len, i);
            }
        }

    }

    public String swap(String s, int i, int j){
        char temp1;
        char[] chArray = s.toCharArray();
        temp1 = chArray[i];
        chArray[i] = chArray[j];
        chArray[j] = temp1;
        return String.valueOf(chArray);
    }
}


Question 6:

Find duplicate characters in String.

Answer 6:

  • Convert String to char array using String.toCharArray().
  • Put each character in HashSet. If add() method returns false, means, it is a duplicate character and print it.

Question 7:

Count number of vowels and consonants in String.

Answer 7:

  • Convert String to char array using String.toCharArray().
  • Run a for loop and compare each character with 'a', 'e', 'i' , 'o', 'u'.  If character matches with anyone of these, it is a vowel else it is a consonant.


Question 8:

Count occurrence of given character in String.

Answer 8:

  • Convert String to char array using String.toCharArray().
  • Run for loop and before storing character in HashMap, first call get(char) and check the return value.
  • If it is -1, then store character with value 1. Else store it with value++.

Question 9:

How to reverse words in a sentence?

Answer 9:

Sentence: "Javafreeway.blogspot.com" is good tutorial.
Output: "tutorial good is "Javafreeway.blogspot.com"

Steps:
  • Use split() method to get a String array of all words.
  • Now just iterate this array in  reverse order and append words in StringBuffer.

Question 10:

Check if a String is Palindrome.

Answer 10:
A string is palindrome, if reading it from both ends results in same string.
  • Convert a String into char array using String.toCharArray().
  • Run a for loop in reverse order and store each character in StringBuilder or StringBuffer.
  • Now compare original String with this new one. If both are equal , then String is palindrome.


No comments:

Post a Comment