HCLTech Coding Questions: Solve 4 Core Patterns Step by Step

Learn a pattern-first method for coding rounds through four original problems. Trace hashing, sliding windows, stacks and interval scheduling from input to answer.

KnowledgeGate Team

Exam prep & CS education

Updated 27 Oct 20266 min read

Memorising a list labelled "HCLTech questions" breaks as soon as the input changes or the evaluator asks why your approach works. A reusable pattern is more useful because it comes with an invariant, a trace, complexity and edge cases. These four problems are representative practice, not an official or fixed HCLTech question bank. We will dry-run hashing, sliding windows, stacks and greedy selection, then build a timed answer method. Use Company-Specific Placement Courses to place this work inside wider company-wise preparation.

HCLTech coding questions: use a five-part answer

Clarify tie-breaking, duplicates, empty input, mutation permission and result type before coding. Use clarify output -> state simple approach -> identify the pattern -> trace one input -> give complexity and tests.

Prompt clue

Likely pattern

State to maintain

Invariant

find a complement or frequency

hash map

value to index or count

stored state covers only processed items

longest valid contiguous range

sliding window

left boundary plus last-seen index

current window satisfies the rule

nested opening and closing symbols

stack

unmatched opening symbols

stack order matches nesting

maximum number of compatible intervals

greedy after sorting

end time of last selection

chosen prefix leaves the most room

Compare with TCS Coding Questions: 4 Patterns with Solved Dry Runs. Neither article defines an official employer topic list.

Hash-map pattern: return the first pair that sums to 10

Prompt: Given [3, 8, 4, 7, 2] and target 10, return the indices of the first pair found by a left-to-right scan whose values sum to 10. Return immediately when the current value's complement has already appeared. This contract does not enumerate all pairs.

Code
seen = {}
for each index i:
    need = target - a[i]
    if need exists in seen:
        return (seen[need], i)
    seen[a[i]] = i

The complete processed trace is:

  • i=0, value=3, need=7, store {3:0}.

  • i=1, value=8, need=2, store {3:0,8:1}.

  • i=2, value=4, need=6, store {3:0,8:1,4:2}.

  • i=3, value=7, need=3, find index 0.

Return indices (0,3) and values (3,7) because 3 + 7 = 10. Do not process index 4 after returning. Expected time is O(n) and worst-case auxiliary space is O(n). For [5,5] with target 10, lookup before insertion lets index 1 find index 0, giving (0,1). An all-pairs contract is different and would also include values 8 and 2 in the main input.

Two-sum hash-map dry run for array [3, 8, 4, 7, 2] and target 10. Draw five indexed cells labelled 0:3, 1:8, 2:4, 3:7, 4:2. Under indices 0 through 3 show rows exactly i=0, need=7, seen={}, then store 3:0; i=1, need=2, seen={3:0}, then store 8:1; i=2, need=6, seen={3:0,8:1}, then store 4:2; i=3, need=3, seen={3:0,8:1,4:2}, found 3 at index 0. Highlight the returned indices (0,3), returned values (3,7), and check 3+7=10. Grey out index 4:2 with the label not processed after return. Do not change any value, map state, index or return pair.

Sliding-window pattern: longest substring without repetition

Prompt: For s = "abcaefbg", return the longest contiguous substring containing no repeated character. Ties retain the earliest best window because we update the answer only for a strictly longer window. Track left, right, a lastSeen map and the best window:

  • r=0 a -> [0,0] "a", best 1

  • r=1 b -> [0,1] "ab", best 2

  • r=2 c -> [0,2] "abc", best 3

  • r=3 a, previous 0 -> left=1, window "bca", best 3

  • r=4 e -> "bcae", best 4

  • r=5 f -> "bcaef", best 5

  • r=6 b, previous 1 -> left=2, window "caefb", best 5

  • r=7 g -> "caefbg", best 6

The answer is substring "caefbg", indices 2..7, length 6. Update with left = max(left, lastSeen[ch] + 1), so a repeat outside the current window cannot move left backwards. Each character is processed once, giving O(n) time and O(min(n, alphabet size)) map space. An empty string returns length 0 and substring "". For "aaaa", strict updates retain the earliest "a" of length 1.

Sliding-window trace for s = "abcaefbg". Draw characters with indices 0:a, 1:b, 2:c, 3:a, 4:e, 5:f, 6:b, 7:g. Show the window after each right index exactly as [0,0] a, [0,1] ab, [0,2] abc, [1,3] bca, [1,4] bcae, [1,5] bcaef, [2,6] caefb, [2,7] caefbg. Mark the left-boundary jumps 0 -> 1 when the second a appears at index 3 and 1 -> 2 when the second b appears at index 6. Highlight final indices 2..7, substring caefbg, and length 6. Do not invent another character, window or length.

Stack pattern: validate nested brackets without losing order

Use input "{[()]}()" with mappings ')' -> '(', ']' -> '[' and '}' -> '{'. Read {, push, depth 1; read [, push, depth 2; read (, push, depth 3; read ), pop (, depth 2; read ], pop [, depth 1; read }, pop {, depth 0; read (, push, depth 1; read ), pop (, depth 0. The empty stack at the end means valid.

Now trace "([)]": push (, push [, then at index 2 the closing ) requires ( but the top is [, so return false immediately. A closing symbol also fails if the stack is already empty. A non-empty stack after the final character fails too.

The algorithm takes O(n) time and O(n) worst-case stack space. Its invariant is exact: after any processed prefix, the stack contains precisely the unmatched opening symbols in their nesting order.

Greedy pattern: schedule the maximum compatible tasks

Let tasks be half-open intervals, so a task ending at t is compatible with one starting at t: A=[1,3), B=[2,5), C=[4,6), D=[6,7), E=[5,8), F=[8,9). We need a maximum-size compatible subset. Sorting by end time gives A, B, C, D, E, F.

The full earliest-finish trace is:

  • Select A; set lastEnd=3.

  • Skip B because 2 < 3.

  • Select C because 4 >= 3; set lastEnd=6.

  • Select D because 6 >= 6; set lastEnd=7.

  • Skip E because 5 < 7.

  • Select F because 8 >= 7; set lastEnd=9.

Return [A,C,D,F], count 4. Earliest finish is correct for unweighted interval scheduling: replace the first interval of any optimal solution with one that finishes no later, and the remaining room cannot shrink. Sorting takes O(n log n) time. The returned list uses O(n) output space, while selection needs O(1) extra state apart from sorting. Weighted intervals need dynamic programming, not this greedy rule.

Coding-round traps: test the invariant, not only the sample output

Pattern

Common wrong move

Counterexample

Fix

hash map

insert before checking

[5,5], target 10 can pair an item with itself in careless code

check complement before insert

sliding window

move left backwards

a repeat whose last index is before left

use max(left, lastSeen+1)

stack

compare against any opener

([)]

compare only with the top

interval greedy

choose shortest duration

a short late task can block more future tasks

choose earliest finish for unweighted scheduling

Author-designed, not an HCLTech rule: 5 minutes clarify both prompts + 15 minutes solve the first + 18 minutes solve the second + 7 minutes test and explain = 45 minutes. Coding Round Strategy: Triage, Time-Box, Bank Marks explains execution.

For oral drills, use DSA Interview Questions: 7 Patterns for Placements. Finish with normal, boundary and adversarial cases, plus spoken complexity.

HCLTech coding test context: separate official facts from practice patterns

Within the Ascend assessment route described on HCLTech's official campus-hiring page, the current page lists a coding or technical test lasting 60 to 90 minutes. It also says candidates who clear its cognitive and coding tests proceed to Versant, followed by technical and HR interviews.

That page does not turn these four practice problems into an HCLTech syllabus or promise that every role follows the same route. Read that route-specific scope narrowly. Check your current job description, recruiter mail, campus notice and test-platform instructions for permitted languages and libraries, input format, timing and sections. Those current instructions control your assessment.

HCLTech coding questions: the short version and next step

A complement or count suggests a hash map. A longest valid contiguous range suggests a sliding window. Nested matching suggests a stack. The maximum count of compatible unweighted intervals suggests earliest-finish greedy. Recognition is incomplete until you can state the invariant, trace an input, give complexity and test a failure case.

Use Coding For Placements if a guided C, C++, Java, Python and competitive-coding route helps. Otherwise, rerun these four examples from blank paper and change one input value in each.