Persistent Systems Coding Questions: Three Patterns with Solved Examples

Recognise when to use complement lookup, a sliding window or a running best sum. Work through three Python solutions and the edge cases that expose common mistakes.

KnowledgeGate Team

Exam prep & CS education

Updated 27 Nov 20265 min read
Persistent Systems Coding Questions: Three Patterns with Solved Examples

A useful coding practice block ties each prompt to an algorithm, an invariant and edge-case tests. For Persistent Systems preparation, these three patterns turn a changed input into a deliberate choice of state and method.

Recognise the operation before choosing an algorithm

Start with what the answer must represent:

Request

Method

Two indices whose values sum to a target

Complement lookup

Longest contiguous substring without repetition

Sliding window

Maximum-sum non-empty contiguous subarray

Running best sum

A substring or subarray is contiguous; a subsequence may skip positions. Here, indices are zero-based, pair indices must differ, strings are case-sensitive, and maximum subarrays cannot be empty.

Check constraints before coding. For 100,000 elements, checking every pair means n(n-1)/2 = 100,000 x 99,999 / 2 = 4,999,950,000 pairs, about five billion. Programming foundations should include arrays, maps and complexity analysis.

Pair sum: store the complement you will need

Given [8, -3, 11, 5] and target 8, return any two distinct indices whose values add to eight. Return (-1, -1) if no pair exists.

For each value x, look for target - x among earlier values. Store each value against its index:

python
def pair_sum(nums, target):
    seen = {}
    for i, x in enumerate(nums):
        need = target - x
        if need in seen:
            return (seen[need], i)
        seen[x] = i
    return (-1, -1)

At index 0, 8 - 8 = 0 is absent, so store 8:0. At index 1, 8 - (-3) = 11 is absent, so store -3:1. At index 2, 8 - 11 = -3 is present at index 1: return (1, 2) because -3 + 11 = 8.

The invariant is that the map contains only earlier indices. Checking before insertion makes [3,3], target 6, return (0,1), while [3] returns no pair. Inserting first could match the current element with itself.

Pair checking costs O(n^2) time. This scan takes expected O(n) time and O(n) space: hash lookup has expected constant cost, not an unconditional worst-case guarantee.

Longest unique substring: move the left boundary forward

Find the length of the longest contiguous substring without repeated characters in "tmmzuxt". The longest valid window is "mzuxt" with length 5.

Maintain a window [left, right] and each character's last-seen index. A repeated character requires moving beyond its previous occurrence only if that occurrence is inside the window.

python
def longest_unique(s):
    left = best = 0
    last_seen = {}
    for right, ch in enumerate(s):
        if ch in last_seen:
            left = max(left, last_seen[ch] + 1)
        last_seen[ch] = right
        best = max(best, right - left + 1)
    return best

right

Character

Previous index

left after update

Window

Length

best

0

t

None

0

t

1

1

1

m

None

0

tm

2

2

2

m

1

max(0,2) = 2

m

1

2

3

z

None

2

mz

2

2

4

u

None

2

mzu

3

3

5

x

None

2

mzux

4

4

6

t

0

max(2,1) = 2

mzuxt

5

5

At right = 6, blindly setting left = last_seen[ch] + 1 for the final t would move left backwards to 1 and accept "mmzuxt", which contains a repeated m.

The invariant is that the current window contains no repeated characters and left never decreases. Each right boundary is processed once: expected O(n) time with hash-map operations, using O(min(n, alphabet size)) stored entries. The empty string returns 0; "aaaa" returns 1.

Maximum subarray: decide whether to extend or restart

Find the maximum sum of a non-empty contiguous subarray of [5, -2, 3, -1, 2, -6, 4].

Let ending mean the best sum ending exactly at the current index. Either extend the previous subarray or restart at the current value. Let best retain the largest sum found anywhere so far.

python
def maximum_subarray(nums):
    if not nums:
        raise ValueError("Input must be non-empty")
    ending = best = nums[0]
    for i in range(1, len(nums)):
        x = nums[i]
        ending = max(x, ending + x)
        best = max(best, ending)
    return best

Initialise both sums to 5. At index 1, restart: max(-2, 5 - 2) = 3, so best stays 5. At index 2, extend: max(3, 3 + 3) = 6, and best becomes 6. At index 3, max(-1, 6 - 1) = 5. At index 4, max(2, 5 + 2) = 7, so best becomes 7. At index 5, max(-6, 7 - 6) = 1; at index 6, max(4, 1 + 4) = 5. The final best remains 7.

The answer is 7, from [5, -2, 3, -1, 2] at indices 0 through 4. The function returns the sum. It takes O(n) time and O(1) extra space.

For [-5,-2,-7], the answer is -2. Initialising best to zero would wrongly permit an empty subarray.

Hidden-test traps: check the contract, not only the sample

Use tests that target the reason your algorithm works:

Problem

Failing test

Repair

Pair reuses one index

[3], target 6

Check before insertion; return (-1,-1)

Window moves backwards

"tmmzuxt"

Use max for left; answer 5

Empty subarray wins

[-5,-2,-7]

Initialise from the first element; answer -2

Also check negative values: [-5, 9, 2], target 4, returns (0, 1) because -5 + 9 = 4. For [1,2], target 8, return (-1,-1).

Two values of 2,000,000,000 sum to 4,000,000,000. Fixed-width languages need a sufficiently wide integer type for that arithmetic; Python integers avoid this fixed-width overflow issue.

Empty inputs give no pair, substring length zero, and an error for the non-empty subarray contract. Separate the returned result from required printed output. Omit debug labels when the judge expects only numbers.

Applying these patterns in a Persistent Systems assessment

For employer-specific pattern sets, compare HCLTech Coding Questions: 4 Patterns with Step-by-Step Solutions, Coforge Coding Questions: Patterns with Solved Examples, Dell Technologies Coding Questions: Three Patterns with Solved Examples and JPMorgan Chase Coding Questions: Patterns with Solved Examples. Their examples keep their own employer devices and language choices; the inputs here are fresh Python traces, and the third worked pattern focuses on maximum-subarray restart semantics.

Practise this sequence: read constraints and output requirements, choose a method, explain its invariant, trace an edge case, then implement and test. The explanation should identify why discarded information cannot improve the answer.

Use Persistent Systems' official careers portal for current opportunities, and follow the assessment instructions in your invitation for your role.

For personal practice, try five minutes reading, fifteen coding and ten testing: 5 + 15 + 10 = 30 minutes. This is a practice block, not company timing.

The short version and the next practice step

Pair sum reuses earlier information. The substring algorithm keeps a valid window. Maximum subarray retains the best ending sum. Explain why each update is safe before running the code.

Now change the pair-sum target to 8 on [8,-3,11,5]. The same scan returns (1,2) because -3 + 11 = 8.

For structured language and algorithm practice, Coding For Placements covers C, C++, Java and Python, with concept videos, practice tests, and data structures and algorithms.