SDET JAVA CODING QUESTIONS
1. First Non-Repeated Character in a String
Why it's asked: To test your ability to analyze string patterns, use maps efficiently, and write clean traversal
logic - useful for validating text payloads, error logs, or API responses.
How to Approach:
- Use a LinkedHashMap to maintain the count and order of characters.
- Traverse the map to find the first character with a count of 1.
Example:
Input: "swiss"
Output: w
Code in Java:
public class FirstUniqueChar {
public static Character firstNonRepeated(String str) {
Map<Character, Integer> countMap = new LinkedHashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for ([Link]<Character, Integer> entry : [Link]()) {
if ([Link]() == 1) return [Link]();
}
return null;
}
public static void main(String[] args) {
[Link](firstNonRepeated("swiss")); // Output: w
}
}
2. Longest Substring Without Repeating Characters
Why it's asked: To assess your understanding of the sliding window technique and handling edge cases -
important when validating uniqueness in session IDs, tokens, or form inputs.
How to Approach:
- Use a sliding window approach with a HashSet to track characters.
- Expand the window until you hit a duplicate, then slide the start.
Example:
Input: "abcabcbb"
[Link]/in/vishnupriyaravichandran
Output: 3 ("abc")
Code in Java:
public class LongestSubstring {
public static int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
int left = 0, max = 0;
for (int right = 0; right < [Link](); right++) {
while ([Link]([Link](right))) {
[Link]([Link](left++));
}
[Link]([Link](right));
max = [Link](max, right - left + 1);
}
return max;
}
public static void main(String[] args) {
[Link](lengthOfLongestSubstring("abcabcbb")); // Output: 3
}
}
3. Valid Parentheses
Why it's asked: To evaluate your grasp of stack data structures and logic matching - especially relevant for
testing parsers, validating JSON/XML formats, or building custom validators.
How to Approach:
- Use a stack to track opening brackets.
- For each closing bracket, check if it matches the top of the stack.
Example:
Input: "()[]{}"
Output: true
Code in Java:
public class ValidParentheses {
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char c : [Link]()) {
if (c == '(' || c == '{' || c == '[') {
[Link](c);
} else {
if ([Link]()) return false;
char open = [Link]();
[Link]/in/vishnupriyaravichandran
if ((c == ')' && open != '(') || (c == '}' && open != '{') || (c
== ']' && open != '['))
return false;
}
}
return [Link]();
}
public static void main(String[] args) {
[Link](isValid("()[]{}")); // Output: true
}
}
4. Reverse Words in a String
Why it's asked: To test string manipulation skills - often needed in test data generation, URL parsing, or
response formatting in automation tasks.
How to Approach:
- Split the string by spaces.
- Reverse the words and join them with a single space.
Example:
Input: " the sky is blue "
Output: "blue is sky the"
Code in Java:
public class ReverseWords {
public static String reverseWords(String s) {
String[] words = [Link]().split("\s+");
[Link]([Link](words));
return [Link](" ", words);
}
public static void main(String[] args) {
[Link](reverseWords(" the sky is blue ")); // Output:
"blue is sky the"
}
}
5. Check if Two Strings are Anagrams
Why it's asked: To check how well you use character maps or sorting for comparison - useful in test
automation where two API responses or payloads need to be validated for logical equality.
How to Approach:
[Link]/in/vishnupriyaravichandran
- Sort both strings and compare.
- Alternatively, use a frequency counter with a HashMap.
Example:
Input: s = "listen", t = "silent"
Output: true
Code in Java:
public class AnagramCheck {
public static boolean isAnagram(String s, String t) {
if ([Link]() != [Link]()) return false;
int[] count = new int[26];
for (char c : [Link]()) count[c - 'a']++;
for (char c : [Link]()) {
if (--count[c - 'a'] < 0) return false;
}
return true;
}
public static void main(String[] args) {
[Link](isAnagram("listen", "silent")); // Output: true
}
}
6. Find the Missing Number
Why it's asked: To evaluate problem-solving using formulas or bitwise XOR - helps in validating test case
completeness, detecting gaps in sequence data, or test coverage checks.
How to Approach:
- Use sum formula: n(n+1)/2 - sum of array.
- OR use XOR approach for better performance.
Example:
Input: [3, 0, 1]
Output: 2
Code in Java:
public class MissingNumber {
public static int missingNumber(int[] nums) {
int n = [Link];
int total = n * (n + 1) / 2;
for (int num : nums) total -= num;
return total;
}
[Link]/in/vishnupriyaravichandran
public static void main(String[] args) {
[Link](missingNumber(new int[]{3, 0, 1})); // Output: 2
}
}
7. Move Zeroes
Why it's asked: To test in-place array manipulation using two-pointer approach - useful for preparing data for
validation, removing nulls, or shuffling test results efficiently.
How to Approach:
- Use two-pointer approach to swap non-zero elements forward.
- Fill the rest with zeroes.
Example:
Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
Code in Java:
public class MoveZeroes {
public static void moveZeroes(int[] nums) {
int index = 0;
for (int i = 0; i < [Link]; i++) {
if (nums[i] != 0) {
nums[index++] = nums[i];
}
}
while (index < [Link]) {
nums[index++] = 0;
}
}
public static void main(String[] args) {
int[] arr = {0, 1, 0, 3, 12};
moveZeroes(arr);
[Link]([Link](arr)); // Output: [1, 3, 12, 0, 0]
}
}
8. Implement a Queue Using Stacks
Why it's asked: To assess understanding of core data structures and system design principles - important in
building custom frameworks, queuing mechanisms, or execution engines.
How to Approach:
[Link]/in/vishnupriyaravichandran
- Use two stacks: input for push, output for pop/peek.
- Transfer elements when output stack is empty.
Example:
Operations: push(1), push(2), peek(), pop()
Output: 1, 1
Code in Java:
class MyQueue {
Stack<Integer> input = new Stack<>();
Stack<Integer> output = new Stack<>();
public void push(int x) {
[Link](x);
}
public int pop() {
peek();
return [Link]();
}
public int peek() {
if ([Link]()) {
while (![Link]()) {
[Link]([Link]());
}
}
return [Link]();
}
public boolean empty() {
return [Link]() && [Link]();
}
public static void main(String[] args) {
MyQueue q = new MyQueue();
[Link](1);
[Link](2);
[Link]([Link]()); // Output: 1
[Link]([Link]()); // Output: 1
}
}
[Link]/in/vishnupriyaravichandran
9. Find the Majority Element
Why it's asked: To test your ability to detect dominant patterns - useful in analyzing test logs, predicting
dominant failures, or creating frequency-based validation scripts.
How to Approach:
- Use Boyer-Moore Voting Algorithm.
- Keep count and candidate, adjust as you traverse.
Example:
Input: [2, 2, 1, 1, 1, 2, 2]
Output: 2
Code in Java:
public class MajorityElement {
public static int majorityElement(int[] nums) {
int count = 0, candidate = 0;
for (int num : nums) {
if (count == 0) candidate = num;
count += (num == candidate) ? 1 : -1;
}
return candidate;
}
public static void main(String[] args) {
[Link](majorityElement(new int[]{2,2,1,1,1,2,2})); //
Output: 2
}
}
10. Find the Intersection of Two Arrays
Why it's asked: To evaluate your efficiency in using sets/maps - essential in comparing test input/output,
finding common elements in APIs, or overlapping data validations.
How to Approach:
- Use sets to collect unique intersection elements.
- Traverse and check membership.
Example:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Code in Java:
public class ArrayIntersection {
[Link]/in/vishnupriyaravichandran
public static int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
for (int num : nums1) [Link](num);
Set<Integer> resultSet = new HashSet<>();
for (int num : nums2) {
if ([Link](num)) [Link](num);
}
return [Link]().mapToInt(i -> i).toArray();
}
public static void main(String[] args) {
[Link]([Link](intersection(new int[]{1,2,2,1}, new
int[]{2,2}))); // Output: [2]
}
}
[Link]/in/vishnupriyaravichandran