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

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. The four worked drills use prefix sums, character frequency, interval merging, and matrix traversal. They are representative drills, not a claim about a fixed current Cognizant paper. Use Placement Preparation & Coding when you want recruiter-focused preparation.
Cognizant coding questions: learn the pattern, not a memorised answer
Signal in the prompt | Useful structure | Worked example | 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:
What is the exact output?
Can values be negative or repeated?
What input size must the approach handle?
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 focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.
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 focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.
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 bestTime and space are O(n). Sliding windows for positive numbers fail because negatives reduce the sum.
![Prefix-sum table for A = [3, -1, 2, 5, -2, 3], with two highlighted length-five subarrays that each sum to 7.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1786681332407_9b8a0t.jpg)
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 Wipro Coding Questions post focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.
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) focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.
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 focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.
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] focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.

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) focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.
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 <= bottomandleft <= rightbefore reverse traversalscall 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
5character comparison -> frequency differences, answer
4 deletionsoverlapping 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 focuses on first non-repeating characters, moving zeroes, and fixed-window sums; this set instead traces prefix sums with negatives, anagram deletions, interval merging, and spiral boundaries.
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.
Keep learning
Cognizant Aptitude Questions: High-Yield Topics with Worked Solutions
Learn how to classify and solve representative mixed aptitude problems, then use a 30-minute drill and five-session plan to turn methods into speed.
Call by Value, Address and Reference Interview Questions with Answers
Trace what changes in the caller and callee for value parameters, pointer parameters and C++ references. The worked C and C++ outputs also cover arrays, structures and pointer reseating.
C++ Value Categories and Move Semantics: Trace What Actually Moves
Trace a unique allocation through lvalues, xvalues, prvalues, copy and move constructors, named rvalue references, const, and C++17 copy elision.
Call by Value, Address and Reference: C and C++ Worked Examples
Learn what C actually copies during a function call, how pointers still change caller-owned data, and where C++ references differ. Every case is traced to its final value.





