Back to SDET Scenarios
Q4Question 4 of 12

Common SDET coding tasks with complexity and edge cases

Key Concepts
  • First non-repeating character: use an insertion-ordered count map, two passes, O(n) time and space
  • Reverse a string without built-ins: two-pointer swap or index loop from end, O(n) time
  • Remove array duplicates: add to a set for O(n), or iterate with a seen-set to preserve order
  • Dedupe objects by a property: keep a set of seen keys and include an object only the first time its key appears
  • Second-highest salary in SQL: DENSE_RANK() OVER (ORDER BY salary DESC) where rank = 2, handles ties naturally
  • The interviewer values reasoning over the answer: state approach, complexity, edge cases, and trade-offs

Definition

Solve first-non-repeating-char with a count map, reverse a string with a two-pointer or index loop, dedupe arrays with a set and objects with a seen-by-key set, and second-highest salary with DENSE_RANK or a subquery, always stating complexity and edge cases, because the interviewer values your reasoning over the answer.
  • First non-repeating char – Count map (insertion-ordered), two passes, O(n) time, O(k) space.
  • Reverse string – Two-pointer swap or index loop from end, O(n) time, O(n) space for immutable strings.
  • Dedupe array – Add to a set for O(n); use seen-set to preserve order.
  • Dedupe objects by key – Keep a set of seen property values, include an object only the first time its key appears.
  • Second-highest salary – DENSE_RANK() OVER (ORDER BY salary DESC) where rank = 2, or MAX(salary) < MAX(salary) subquery.
  • Reasoning over answer – State approach, complexity, edge cases, and trade-offs because that is what the interviewer assesses.

These are like quick aptitude checks the National Bank of India gives an automation engineer to see how they think, not whether they memorized an answer. Can they find the first customer in a queue who appears only once, reverse an account reference by hand, collapse a list of duplicate transactions, remove duplicate customers by their account number, and work out who earns the second-highest salary even when several people tie. The interviewer watches how you reason: do you pick the right tool, do you say how fast it runs, do you remember the empty list and the all-duplicates cases. A candidate who explains their thinking clearly is more valuable than one who blurts a memorized solution, because the job is thinking through test and data problems all day.

Understanding the Concepts

How do you find the first non-repeating character?

Use an insertion-ordered count map with two passes. O(n) time, O(k) space.

JAVA
package com.bank.coding;

import java.util.LinkedHashMap;
import java.util.Map;

public class FirstNonRepeating {
    public static Character find(String s) {
        if (s == null || s.isEmpty()) return null;
        Map<Character, Integer> counts = new LinkedHashMap<>();
        for (char c : s.toCharArray()) {
            counts.merge(c, 1, Integer::sum);
        }
        for (Map.Entry<Character, Integer> entry : counts.entrySet()) {
            if (entry.getValue() == 1) return entry.getKey();
        }
        return null; // All characters repeat
    }
}
// Edge cases: empty string, all repeats, single character

How do you reverse a string without built-ins?

Two-pointer swap or index loop from end. O(n) time, O(n) space.

JAVA
public static String reverse(String s) {
    if (s == null || s.length() <= 1) return s;
    char[] chars = s.toCharArray();
    int left = 0, right = chars.length - 1;
    while (left < right) {
        char temp = chars[left];
        chars[left] = chars[right];
        chars[right] = temp;
        left++;
        right--;
    }
    return new String(chars);
}
// Edge cases: empty, single char, Unicode surrogate pairs

How do you remove duplicates from an array?

Add to a set for O(n). Use a seen-set to preserve order.

JAVA
public static List<Integer> dedupe(int[] arr) {
    Set<Integer> seen = new LinkedHashSet<>();
    for (int n : arr) seen.add(n);
    return new ArrayList<>(seen);
}
// Edge cases: empty array, all same values, already unique

How do you deduplicate objects by a property?

Keep a set of seen property values, include an object only the first time its key appears.

JAVA
public static List<Account> dedupeByAccountNo(List<Account> accounts) {
    Set<String> seen = new HashSet<>();
    List<Account> result = new ArrayList<>();
    for (Account acc : accounts) {
        if (seen.add(acc.getAccountNo())) {
            result.add(acc);
        }
    }
    return result;
}
// Edge cases: null keys, empty list, all duplicates

How do you find the second-highest salary?

Use DENSE_RANK or a MAX subquery.

SQL
-- DENSE_RANK approach (handles ties, scales to Nth)
SELECT salary FROM (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
  FROM employees
) ranked
WHERE rnk = 2;

-- Subquery approach (simpler for exactly second)
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

-- Both return NULL when only one distinct salary exists

How do you handle ties and edge cases in SQL?

DENSE_RANK handles ties naturally. ROW_NUMBER does not.

SQL
-- DENSE_RANK: tied salaries share a rank
-- Salaries: 100000, 80000, 80000, 60000
-- Ranks:    1,       2,     2,     3
-- Second highest: 80000 (both rows)

-- ROW_NUMBER: each row gets a unique number
-- Salaries: 100000, 80000, 80000, 60000
-- Numbers:  1,       2,     3,     4
-- "Second row": only one of the 80000s

-- Edge case: single salary
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
-- Returns NULL (correct, there is no second)

Why does the interviewer care about reasoning?

These problems are proxies for daily SDET work: decomposition, data structures, complexity, edge cases.

TEXT
  Strong answer:
  "I use a LinkedHashMap for two reasons: first, it gives O(1)
   lookups for the count, and second, it preserves insertion order
   so I can find the FIRST non-repeating character. The time
   complexity is O(n) for two passes. Edge cases: empty string
   returns null, all-repeating returns null."

  Weak answer:
  "I loop through and check each character." (no complexity,
   no data structure justification, no edge cases)

What are common interview follow-up questions?

  • "What if the string is very long?" The two-pass approach is still O(n), which is optimal. You cannot do better.
  • "Can you do the SQL one without window functions?" Yes, use the MAX subquery approach. It works on older databases.
  • "Which duplicate do you keep?" The first encountered, because we use a seen-set that rejects subsequent duplicates.

Key Takeaway

I solve these four tasks the same disciplined way, stating the approach, the complexity, and the edge cases. For the first non-repeating character I make two passes: the first counts each character into an insertion-ordered map, the second returns the first character with a count of one, which is linear time and linear in the distinct characters, and the edge cases are an empty string and an all-repeating string, both of which have no answer, plus clarifying case sensitivity. For reversing a string without built-ins I convert to a character array and swap from both ends inward with two pointers, linear time and space, with empty and single-character strings returning themselves, and I mention that naive character swapping can corrupt combined Unicode as a thoughtful nuance. For removing duplicates from an array I add each element to a set and keep it only on first sight so order is preserved, linear time and space, and the follow-up to dedupe objects by a property cannot use object equality because two distinct objects can share a property, so I keep a set of seen key values and include an object only the first time its key appears. For the second-highest salary I use DENSE_RANK ordered descending and select rank two, which handles ties by treating tied salaries as one rank and returns the second distinct salary, or the classic subquery selecting the maximum salary below the overall maximum, and I note that a single distinct salary yields null, and that if second-highest must mean the second row rather than the second distinct value I would use ROW_NUMBER instead.

The reason the interviewer cares about my reasoning more than the answer is that these problems are proxies for the daily work of an SDET: constant problem decomposition, choosing the right data structure, reasoning about complexity when a test must process large data, and anticipating edge cases, which is exactly what good testing is. A candidate who clarifies the ambiguous requirement, justifies the data structure, states the complexity out loud, and names the edge cases proactively demonstrates the thinking that transfers to designing tests, debugging failures, and writing framework utilities, whereas a memorized answer with no explanation predicts nothing about on-the-job competence.

At the National Bank of India these exact patterns showed up in real work, dedupe-by-property when collapsing duplicate customer records by account number, and a DENSE_RANK query for ranking, and in each case the valuable part was reasoning about ties, nulls, and order, not recalling syntax. So in an interview I deliberately narrate my thinking, clarify, choose, analyze, and enumerate edges, because showing how I reason is the thing actually being assessed.

Remember one sentence: state your approach, the complexity, and the edge cases for every coding task, because the interviewer values your reasoning more than the answer.

---

Visual Overview

DIAGRAM
  SDET Coding Tasks Summary
  ──────────────────────────

  Task                    │ Approach           │ Time │ Space
  ────────────────────────┼────────────────────┼──────┼──────
  First non-repeating char│ Count map (ordered)│ O(n) │ O(k)
  Reverse string          │ Two-pointer swap   │ O(n) │ O(n)
  Dedupe array            │ Set                │ O(n) │ O(n)
  Dedupe objects by key   │ Seen-key set       │ O(n) │ O(n)
  2nd highest salary      │ DENSE_RANK or MAX  │  -   │  -
Share
Use ← → arrow keys to navigate