Hexaware Coding Questions: 4 Patterns with Worked Examples
Learn four coding patterns through exact dry runs: a reverse scan, sliding window, prefix-suffix products and spiral traversal. Each example includes complexity and edge cases.
KnowledgeGate Team
Exam prep & CS education

Searches for Hexaware coding questions often mix recycled lists, old assessment formats and unsupported claims. The useful response is to practise four reusable patterns with fixed inputs, exact outputs, dry runs, complexity checks and edge cases. Hexaware's official jobs portal lists current opportunities but does not publish a permanent coding-question blueprint. Knowledge Gate's IT placements and company-specific preparation page describes broad programming-fundamentals and coding preparation. These are representative practice problems, not leaked questions or guaranteed repeats.
Hexaware Coding Questions: The Four Clues to Recognise
Start by translating the wording into a method.
Clue in the problem | Method | Target time |
|---|---|---|
Greater than every value to the right | Right-to-left scan with |
|
Longest contiguous segment with no repetition | Sliding window with |
|
Answer for each index using all other values | Prefix and suffix accumulation |
|
Visit an outer layer, then move inward | Four matrix boundaries |
|
Use the same solving contract each time. Write the exact input and output. Identify the smallest state that must survive between iterations. Dry-run that state on the supplied values, then test duplicates, minimum-size input and boundary collapse. If you need a wider catalogue beyond these four patterns, revise DSA interview questions for placements.
The key skill is recognition. Once the clue identifies the state, the implementation becomes much shorter and easier to check.
The earlier Tech Mahindra Coding Questions: 4 Patterns with Solved Examples guide focuses on a broader pattern survey and uses abcaefbb for its sliding-window trace. This Hexaware set uses a different worked set: array leaders, product except self and spiral traversal, with cabcaefg for its unique-substring trace.
Pattern 1: Scan an Array from the Right to Find Leaders
An element is a leader only when it is strictly greater than every element to its right. The last element is always a leader. For [16, 17, 4, 3, 5, 2], the required output is [17, 5, 2] in the original order.
Scan from index 5. Keep 2, so maxRight=2. At index 4, 5>2, so keep 5 and set maxRight=5. Skip 3 and 4. At index 1, 17>5, so keep 17 and set maxRight=17. Skip 16. The scan collects [2, 5, 17]; reverse it once.
leaders = []
maxRight = -infinity
for value from right to left:
if value > maxRight:
append value to leaders
maxRight = value
reverse leadersTime is O(n) and output space is O(k) for k leaders. Check [7] -> [7], [5, 5, 3] -> [5, 3], and [9, 7, 4] -> [9, 7, 4]. Using >= is a common trap because equal values are not strictly greater.
Pattern 2: Use a Sliding Window for the Longest Unique Substring
Given s = "cabcaefg", return the best length 6 and one best substring, "bcaefg", at indices 2..7. Keep left=0 and a lastSeen map. At each right, move left to max(left, lastSeen[ch] + 1) when the character has appeared, record its new index, then compare right-left+1 with the best length.
Indices 0,1,2 form "c", "ca", "cab", giving best lengths 1,2,3. The repeated c at index 3 moves left from 0 to 1, leaving "abc". The repeated a at index 4 moves left to 2, leaving "bca". Indices 5,6,7 extend the window to "bcae", "bcaef", then "bcaefg". The final answer is length 6.

Each character enters the window once, so time is O(n). The map uses O(min(n, alphabet size)) space.
Pattern 3: Build Product Except Self with Prefix and Suffix State
For [2, 3, 4, 5], produce [60, 40, 30, 24] without division. Each answer contains the product of all values except the one at that position.
Build left products that exclude the current value: [1, 2, 6, 24]. The matching right products are [60, 20, 5, 1]. Multiplying corresponding entries gives [60, 40, 30, 24].
For the space-optimised version, initialise the output with the left products and set rightProduct=1. At index 3, write 24*1=24, then update rightProduct to 5. At index 2, write 6*5=30, then update it to 20. At index 1, write 2*20=40, then update it to 60. At index 0, write 1*60=60. The final running product becomes 120, but the output is already complete.
![Prefix-suffix product table for [2,3,4,5] shows products excluding each index, outputs [60,40,30,24], and right-to-left running products.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1786825413213_nleyh1.jpg)
Time is O(n) with O(1) extra space excluding the output. Zeros still work: [1, 2, 0, 4] -> [0, 0, 8, 0], while [1, 0, 3, 0] -> [0, 0, 0, 0].
Pattern 4: Shrink Four Boundaries for Spiral Matrix Traversal
Use this 3 x 4 matrix:
2 4 6 8
5 7 9 1
3 0 2 4Start with top=0, bottom=2, left=0, right=3. Read the top row as 2,4,6,8, then set top=1. Read the right column as 1,4, then set right=2. Read the bottom row backwards as 2,0,3, then set bottom=1. Read the left column upwards as 5, then set left=1. The remaining inner row is 7,9.
The complete output is [2, 4, 6, 8, 1, 4, 2, 0, 3, 5, 7, 9]. Before the bottom pass, check top <= bottom. Before the left pass, check left <= right. These guards stop a collapsed layer from being emitted twice.
Test the shapes that expose boundary errors: [[1,2,3]] -> [1,2,3], [[1],[2],[3]] -> [1,2,3], and [[9]] -> [9]. The traversal takes O(rows * columns) time and O(1) state excluding the output.
How an Online Coding Assessment Tests the Same Methods
Nearby variants test whether you understand the method rather than one memorised answer. Leaders can become stock-span-style left or right dominance. A unique-substring problem can allow at most k distinct characters. Product except self can introduce one or two zeros. Spiral traversal can start from another corner or ask for the kth visited value. Constraints tell you whether a one-pass method is necessary.
Before submitting, check these traps:
Did you decide strict versus non-strict comparison before coding?
Can your sliding window handle
"abba" -> length 2without movingleftbackwards?Does product except self handle
[1,2,0,4] -> [0,0,8,0]without division?Did you preserve each prefix value until it was consumed?
Can a single matrix row or column be visited only once?
Use a clear triage and time-boxing workflow so one stubborn case does not consume the whole assessment. For a separate company-specific practice set with different examples, work through TCS Coding Questions: Patterns and Solved Examples.
The Short Version and a 45-Minute Hexaware Practice Set
Remember the four cues: reverse scan, sliding window, prefix-suffix, shrinking boundaries. Spend 5 minutes reading all four problems, 10 on leaders, 12 on the unique-substring trace, and 10 on either product except self or spiral traversal. Use 5 minutes for the remaining problem's pseudocode and 3 minutes to rehearse one edge case from each pattern. Total: 45 minutes.
Confirm the current role and process on Hexaware's official jobs portal. Use Hexaware Preparation for broader company-specific study, or Coding for Placements for deeper C, C++, Java and Python implementation practice.
Keep learning
Hexaware Interview Questions and Answers: Technical, Coding and HR Rounds
Prepare answers that survive follow-up questions. This guide covers CS fundamentals, a worked SQL query, a coding trace, project defence, HR responses and a timed mock.
Hexaware Placement Preparation: A 5-Week Aptitude-to-Interview Study Plan
Turn your current Hexaware drive instructions into a practical five-week schedule. Diagnose gaps, protect 12 hours each week, practise under time and finish with matched simulations.
Hexaware Recruitment Process and Test Pattern: What to Prepare for Each Round
Do not prepare for a generic Hexaware pattern. Build your plan from the current invitation, then practise the skills and explanations each listed stage needs.
Publicis Sapient Coding Questions: Three Patterns with Worked Solutions
Recognising code is easier than explaining why it works. Practise three patterns through Java methods, exact traces, invariants and edge cases before your next coding assessment.





