Interview Questions – Strings, LInkedList, Stacks & Queues

cutout paper composition of bacteria on green background

Strings

String Methods

Useful Method and Description
charAt(int index)Returns the char value at the specified index.
compareTo(String anotherString)Compares two strings lexicographically.
concat(String str)Concatenates the specified string to the end of this string.
contains(CharSequence s)Returns true if and only if this string contains the specified sequence of char values.
equals(Object anObject)Compares this string to the specified object.
getBytes()Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
getBytes(String charsetName)Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
hashCode()Returns a hash code for this string.
indexOf(int ch)Returns the index within this string of the first occurrence of the specified character.
indexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
isEmpty()Returns true if, and only if, length() is 0.
length()Returns the length of this string.
matches(String regex)Tells whether or not this string matches the given regular expression.
regionMatches(int toffset, String other, int ooffset, int len)Tests if two string regions are equal.
replace(CharSequence target, CharSequence replacement)Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
split(String regex)Splits this string around matches of the given regular expression.
startsWith(String prefix)Tests if this string starts with the specified prefix.
substring(int beginIndex)Returns a string that is a substring of this string.
substring(int beginIndex, int endIndex)Returns a string that is a substring of this string.
toCharArray()Converts this string to a new character array.
toLowerCase()Converts all of the characters in this String to lower case using the rules of the default locale.
toString()This object (which is already a string!) is itself returned.
toUpperCase()Converts all of the characters in this String to upper case using the rules of the default locale.
trim()Returns a string whose value is this string, with any leading and trailing whitespace removed.
valueOf(char c)Returns the string representation of the char argument.
valueOf(char[] data)Returns the string representation of the char array argument.
valueOf(double d)Returns the string representation of the double argument.
valueOf(Object obj)Returns the string representation of the Object argument.

Questions

Question – 1: Check if the given String has no duplicate characters

	private static boolean checkIsUniqueModified(String isUnique) {
		boolean[] unique = new boolean[128];
		char[] array = isUnique.toCharArray();
		for (int i = 0; i < array.length; i++) {
			char value = isUnique.charAt(i);
			if (unique[value]) {
				return false;
			} else {
				unique[value] = true;
			}
		}
		return true;
	}

Question – 2: Check if the given String Can be compressed and expressed in shorter form

	private static String compression(String input) {
		char[] array = input.toCharArray();
		StringBuilder builder = new StringBuilder();
		int count = 1;
		for (int i = 0; i < array.length; i++) {
			if (i < array.length -1 && array[i] == array[i + 1]) {
				count ++;
				continue;
			}
			builder.append(array[i]).append(count);
			count = 1;
		}
		return new String(builder);
	}

Question – 3: Check if the given String is a palindrome permutation

	public static void main(String[] args) {
		String input = "aabbddaa";
		Map<Integer, Integer> map = new HashMap<>();
		char[] array = input.trim().toLowerCase().toCharArray();
		for (int i = 0; i < array.length; i++) {
			int sum = 0;
			if (map.get(Integer.valueOf(array[i])) != null) {
				sum = map.get(Integer.valueOf(array[i]));
			}
			map.put(Integer.valueOf(array[i]), sum +1);
		}
		long count = map.values().stream().filter((i) -> {
			if(i % 2 == 0) {
				return false;
			}
			return true;
		}).count();
		if (count > 1) {
			System.out.println(" Not Palliandrome");
		} else {
			System.out.println("Palliandrome");

		}
		
	}

Question – 4: Check if the input String is a permutation of the given String

	private static boolean checkPermutation(String str1, String str2) {
		char[] str1Array = str1.toCharArray();
		Arrays.sort(str1Array);
		char[] str2Array = str2.toCharArray();
		Arrays.sort(str2Array);
		return new String(str1Array).equals(new String(str2Array));
	}

Question – 5: Check if the input String is a rotation of the given String

	private static boolean isStringRotation(String word1, String word2) {
		String combined = word1 + word1;
		return combined.contains(word2);
	}

LinkList

error: Content is protected !!
Scroll to Top