Interview prep and Java refreshers

Some practice questions and solutions implemented in Java

April 27, 2014, 10:48 p.m.

In preparation for upcoming interviews for a Software Engineering position, I have decided to post some practice questions and my solutions to them. If you are able to think of a better way to solve any of the questions I would appreciate hearing it. I chose to implement these solutions in Java as it will be a good refresher since I have not written a Java program in almost 3 years.

Check if a string is a palindrome

In order to check if a string is a palindrome I decided to simply reverse the string and then do a comparison with that and the original string.

public static boolean is_palindrome(String input) {
    //reverse the input string
    String reversed = new StringBuilder(input).reverse().toString();
    return input.equals(reversed);
}

Turn one string into another

In this problem we are tasked with turning one string into another one by listing the missing chars in the first string of the second string. This is done by looping through string "a" and finding the index of each char in string "b" and then deleting the char at that index. Finally we can simply return string "b".

public static String make_string(String a, String b) {
    int index;
    StringBuilder b_string = new StringBuilder(b); //mutable string
    for(int i = 0; i < a.length(); i++) {
        //find the first appearance of character in b_string
        index = b_string.indexOf(Character.toString(a.charAt(i)));
        if(index > -1) {
            b_string.deleteCharAt(index); //remove at index
        }
    }
    return b_string.toString();
}

Comments about Interview prep and Java refreshers

No one has left a comment yet, be the first to voice your opinion on Interview prep and Java refreshers!