Cognizant Coding Questions: 4 High-Value Patterns with Solved Examples

Learn to recognise four reusable coding patterns, trace each solution, justify its complexity, and test the edge cases that often break a correct-looking approach.

KnowledgeGate Team

Exam prep & CS education

Updated 16 Sep 20266 min read

The story may change, but your job stays the same: recognise the data pattern, choose a complexity that fits, and test edge cases under pressure. These four solved problems cover prefix sums, character frequency, interval merging, and matrix traversal. They are representative drills, not a claim about a fixed current Cognizant paper. Use Company-Specific Placement Courses when you want recruiter-focused preparation.

Cognizant coding questions: learn the pattern, not a memorised answer

Signal in the prompt

Useful structure

Pattern in this article

Target complexity

contiguous range with a target sum and possible negative values

prefix sum + earliest-index hash map

longest subarray summing to 7

O(n)

two strings compared by character counts

frequency array or map

deletions needed for anagrams

O(n + m)

overlapping start-end pairs

sort + one scan

merge five intervals

O(n log n)

all cells in layer order

four shrinking boundaries

3 x 4 spiral traversal

O(rows x columns)

Before coding, ask:

  1. What is the exact output?

  2. Can values be negative or repeated?

  3. What input size must the approach handle?

  4. Which empty, one-item or tie case changes the logic?

See the Cognizant GenC role tracks and 8-week plan for context. KnowledgeGate's Cognizant Superset Course offers practice, not official assessment sections.

Your current assessment invitation controls languages, task count, timing and platform rules.

Cognizant coding pattern 1: longest subarray with sum 7

Practice prompt: Given A = [3, -1, 2, 5, -2, 3] and K = 7, return the maximum length of a contiguous subarray whose sum is K.

Begin with {0: -1} so index-zero subarrays count. Retain each prefix sum's earliest index for the longest candidate.

i

A[i]

prefix

prefix - K

earliest matching index

candidate length

0

3

3

-4

none

none

1

-1

2

-5

none

none

2

2

4

-3

none

none

3

5

9

2

1

3 - 1 = 2

4

-2

7

0

-1

4 - (-1) = 5

5

3

10

3

0

5 - 0 = 5

Indices 0..4 give 3 + (-1) + 2 + 5 + (-2) = 7; indices 1..5 give (-1) + 2 + 5 + (-2) + 3 = 7. Both span five positions, so return 5.

Code
first = {0: -1}; prefix = 0; best = 0
for each index i:
    prefix = prefix + A[i]
    if prefix - K exists in first:
        best = max(best, i - first[prefix - K])
    if prefix is absent from first:
        first[prefix] = i
return best

Time and space are O(n). Sliding windows for positive numbers fail because negatives reduce the sum.

Longest subarray with sum 7. Draw one indexed trace for A = [3, -1, 2, 5, -2, 3] with index row 0, 1, 2, 3, 4, 5 and prefix row 3, 2, 4, 9, 7, 10; place the virtual prefix 0 at index -1 before the array. Highlight exactly two length-5 answers: indices 0..4, values [3, -1, 2, 5, -2], sum 7; and indices 1..5, values [-1, 2, 5, -2, 3], sum 7. Add only these lookup callouts: i=4: prefix 7, need 0 at -1, length 5 and i=5: prefix 10, need 3 at 0, length 5. Footer: answer = 5; time O(n); space O(n). Do not change an index, value, sum, lookup or subarray boundary.

Cognizant coding pattern 2: character frequencies and anagram deletions

Practice prompt: Given s1 = "cde" and s2 = "abc", find the minimum total deletions needed to make the strings anagrams. Assume lowercase English letters. For arbitrary Unicode, use a map instead of the 26-cell array.

Letter

Count in cde

Count in abc

Absolute difference

a

0

1

1

b

0

1

1

c

1

1

0

d

1

0

1

e

1

0

1

Total: 1 + 1 + 0 + 1 + 1 = 4. Delete d, e from cde and a, b from abc, leaving c. Fixed-alphabet time is O(|s1| + |s2|) with O(1) space. Checks: "" and "abc" return 3; two "abc" strings return 0. Continue with Arrays and Strings in Java: 4 Coding-Round Patterns.

Cognizant coding pattern 3: merge overlapping intervals

Practice prompt: Given closed intervals [(1, 3), (2, 6), (8, 10), (9, 12), (15, 18)], merge every overlap. They overlap or touch when next.start <= current.end. Although this input is sorted, the general algorithm first sorts by start.

Begin current = (1, 3). Because 2 <= 3, (2, 6) overlaps and current becomes (1, max(3, 6)) = (1, 6). Since 8 > 6, emit (1, 6) and set current to (8, 10). Because 9 <= 10, merge (9, 12) into (8, 12). Since 15 > 12, emit (8, 12), then (15, 18).

Return [(1, 6), (8, 12), (15, 18)]. Sorting is O(n log n); scanning is O(n). Output space is O(n); sort space is implementation-dependent. Checks: [] -> [], [(4, 7)] -> [(4, 7)], and [(1, 4), (4, 5)] -> [(1, 5)] for closed intervals.

Cognizant coding pattern 4: spiral traversal with shrinking boundaries

Practice prompt: Traverse [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] clockwise. Initialise top = 0, bottom = 2, left = 0, and right = 3.

Pass 1 reads top row 1, 2, 3, 4, then sets top = 1; right column 8, 12, then right = 2; bottom row backwards 11, 10, 9, then bottom = 1; left column upwards 5, then left = 1. Pass 2 has top = bottom = 1 and left = 1, right = 2. It reads 6, 7, sets top = 2, and stops.

Return [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]. Time is O(rows x columns) and space is O(1) excluding output. Guards top <= bottom and left <= right prevent duplicate cells. Checks: [[1, 2, 3, 4]] -> [1, 2, 3, 4] and [[1], [2], [3]] -> [1, 2, 3].

Clockwise spiral of the exact 3 x 4 matrix. Draw three rows with cells 1 2 3 4, 5 6 7 8, and 9 10 11 12. Overlay arrows in exactly this visit order: 1 -> 2 -> 3 -> 4 -> 8 -> 12 -> 11 -> 10 -> 9 -> 5 -> 6 -> 7. Add boundary labels for Pass 1: top=0, bottom=2, left=0, right=3; after the outer layer show Pass 2 boundaries top=1, bottom=1, left=1, right=2. Print the final output exactly as [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]. Do not add cells, diagonal arrows or a second visit to any value.

Cognizant coding questions: choose complexity from constraints

For n = 200, a double loop can make about 200^2 = 40,000 checks. At n = 100,000, it can require about 10,000,000,000. A linear pass processes 100,000 items, while n log2 n is roughly 100,000 x 16.61 = 1,661,000 comparison-scale steps. These illustrate growth, not machine timings or Cognizant limits.

Decisive constraints: negatives rule out an ordinary sliding window; a fixed lowercase alphabet needs 26 counters; unsorted intervals need sorting; outputting every matrix cell cannot beat O(rows x columns).

Explain in order: brute-force idea, why it may not scale, chosen invariant or data structure, time complexity, auxiliary space, one edge case. Justify every Big-O label.

Cognizant coding-round traps: test the claim before submitting

Use the exact mistake, failure, repair chain:

  • use a sliding window despite negative array values -> moving one boundary is no longer monotonic -> use prefix sums and an earliest-index map

  • overwrite an earlier prefix index -> a later match produces a shorter subarray -> retain the first index only

  • merge intervals without sorting -> a later interval can overlap an already-emitted range -> sort by start first

  • omit boundary guards in spiral traversal -> the middle row or column can be appended twice -> check top <= bottom and left <= right before reverse traversals

  • call a practice problem an actual Cognizant question -> creates an unsupported company-specific claim -> label it representative and follow the current assessment invitation for the real format

Say the expected output before running this checklist:

  • compile or run once

  • retest the sample

  • empty input

  • one item

  • duplicates or ties

  • largest allowed input

Cognizant coding questions: the short version and next practice step

  • contiguous target sum with negatives -> prefix sum + earliest index, answer 5

  • character comparison -> frequency differences, answer 4 deletions

  • overlapping ranges -> sort + merge, answer [(1, 6), (8, 12), (15, 18)]

  • layered matrix order -> shrinking boundaries, answer [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]

Create a 75-minute learner drill, not an official simulation: 10 minutes to classify, 45 to implement two, 15 for boundary tests, and 5 to record the first wrong assumption. Total: 10 + 45 + 15 + 5 = 75 minutes.

For C, C++, Java and Python, use Coding for Placements. Use the course above for the company route. Redo them with new values and explain each invariant aloud.