Zoho Coding Questions: 3 Patterns with Step-by-Step Solutions

Know the data structure but get stuck moving indices? Work through three original coding exercises, with loop invariants, complete traces and boundary checks.

KnowledgeGate Team

Exam prep & CS education

Updated 30 Nov 20265 min read
Zoho Coding Questions: 3 Patterns with Step-by-Step Solutions

You recognise an array or string problem, but lose time deciding what to store and when to move an index. These three step-by-step solutions teach those decisions through constraints and a loop invariant, the condition maintained during a loop. Published Accenture Coding Questions: 4 Patterns with Solved Dry Runs owns frequency maps, digit accumulators and a matrix-boundary drill; Cognizant Coding Questions owns the full 3-by-4, 1-to-12 spiral walkthrough. Practise with invented inputs rather than memorising a supposed Zoho question list. Run encoding, duplicate-preserving merge and a boundary-safe spiral invariant give you three data-shape transfers to practise.

Choose a pattern from the problem's constraints

The word “array” does not determine an algorithm. Look for the property that lets you avoid unnecessary work.

Problem property

Useful pattern

Adjacent repeated characters

Scan one consecutive run

Two already sorted arrays

Merge with two pointers

Rectangular matrix perimeter

Shrink four boundaries

Our contracts are case-sensitive ASCII letter strings, ascending integer arrays with duplicates retained, and rectangular matrices traversed clockwise from the top-left. All three solutions return new output without modifying input. The pseudocode is language-neutral.

Before coding, state four things: the contract, a small trace, the implementation and its complexity. Broader placement preparation resources can help you organise practice around these habits.

String scanning: encode consecutive runs

Encode each maximal consecutive run as its character followed by its decimal count, including count 1. For aaabbcaaaa, return a3b2c1a4. Empty input returns an empty string.

Restrict input to letters: this representation does not guarantee unambiguous decoding for arbitrary digit-containing input. It also need not save space: ab becomes a1b1.

Code
encode(s):
    n = length(s)
    i = 0
    outputParts = []
    while i < n:
        j = i + 1
        while j < n and s[j] == s[i]:
            j = j + 1
        append (s[i] + decimal(j - i)) to outputParts
        i = j
    return join(outputParts)

Half-open intervals include the starting index and exclude the ending index:

Run

Count calculation

Output piece

[0,3)

3 - 0 = 3

a3

[3,5)

5 - 3 = 2

b2

[5,6)

6 - 5 = 1

c1

[6,10)

10 - 6 = 4

a4

Diagram of aaabbcaaaa grouped into four runs, with interval bounds, counts 3, 2, 1, 4, and encoded output a3b2c1a4.

The last four a characters cannot join the first three because bbc separates their runs. The invariant is that outputParts encodes exactly the prefix before i.

Each character is scanned once. A parts list joined once gives O(n) time, O(n) worst-case output storage and O(1) index state. Repeatedly extending an immutable result string can invalidate that time bound.

Check a → a1, ab → a1b1, and empty → empty. Continue with Arrays and Strings in Java for related coding practice.

Two pointers: merge sorted arrays without losing duplicates

Merge A=[1,3,3,7] and B=[2,3,6] into a new ascending array. Both inputs are already sorted, so sorting again is unnecessary.

Code
merge(A, B):
    i = 0; j = 0; out = []
    while i < length(A) and j < length(B):
        if A[i] <= B[j]:
            append A[i] to out
            i = i + 1
        else:
            append B[j] to out
            j = j + 1
    while i < length(A):
        append A[i] to out
        i = i + 1
    while j < length(B):
        append B[j] to out
        j = j + 1
    return out

Emitted value

Pointer state (i,j) afterwards

1

(1,0)

2

(1,1)

3 from A

(2,1)

3 from A

(3,1)

3 from B

(3,2)

6

(3,3)

Remaining 7

(4,3)

The result is [1,2,3,3,3,6,7]: four plus three inputs give seven outputs. Equality chooses A first; B's equal value remains available.

The invariant is that output is the sorted merge of consumed prefixes. The smaller unconsumed head is always the next element. Each pointer advances only forwards, giving O(m+n) time, O(m+n) output storage and O(1) pointer state.

Test [] with [2] → [2], and [3] with [3] → [3,3]. A set would silently break the duplicate-preserving contract.

Matrix traversal: shrink four boundaries safely

For [[1,2,3,4],[5,6,7,8],[9,10,11,12]], start with top=0, bottom=2, left=0, right=3. Each completed edge removes part of the unvisited rectangle.

Code
spiral(M):
    if rows(M) == 0 or columns(M) == 0: return []
    top = 0; bottom = rows(M) - 1
    left = 0; right = columns(M) - 1
    out = []
    while top <= bottom and left <= right:
        for c from left to right increasing:
            append M[top][c] to out
        top = top + 1
        for r from top to bottom increasing:
            append M[r][right] to out
        right = right - 1
        if top <= bottom:
            for c from right to left decreasing:
                append M[bottom][c] to out
            bottom = bottom - 1
        if left <= right:
            for r from bottom to top decreasing:
                append M[r][left] to out
            left = left + 1
    return out

Ranges include both endpoints; an empty range emits nothing. The outer edges emit [1,2,3,4], [8,12], [11,10,9], then [5]. Remaining bounds are top=1, bottom=1, left=1, right=2, so the final top edge emits [6,7].

Clockwise spiral through a 3-by-4 matrix numbered 1 through 12, showing outer and inner paths, bounds, and final sequence.

The result contains all 12 cells once. The guards prevent revisiting collapsed rows or columns. For [[1,2,3]], the top edge emits everything; for [[1],[2],[3]], the top emits 1 and the right emits 2,3. Both return [1,2,3].

Each cell is emitted once: O(rows×columns) time and output storage, with O(1) boundary state.

Catch the failures hidden by a happy-path example

Failure

Fix

Global counting combines separated runs into a7

Count adjacent characters only

Deduplication drops required 3 values

Retain every equal element

Unconditional reverse traversal repeats a collapsed edge

Check boundaries before traversing

Test empty and singleton inputs, then duplicate-heavy strings and arrays or single-row and single-column matrices. Distinguish returned output memory from auxiliary working state: constant pointer storage does not mean constant total storage.

Explain why your invariant holds initially, survives each iteration and yields the answer at termination. Apply the same reasoning to DSA interview questions when extending practice to trees and graphs.

Apply these methods to your Zoho assessment instructions

Zoho's careers page is the official starting point for opportunities and candidate registration. Read your role-specific assessment invitation for language, runtime, input-output format, available time and platform rules.

Adapt deliberately. If instructions require only the result, remove debugging labels. If a task asks for distinct values, change the merge contract before changing its implementation. These are hypothetical task variations.

Explain a solution in this order: assumptions, small example, invariant, complexity and edge tests.

The short version: reproduce one solution from scratch

Advance to the end of a run, advance the pointer supplying the next sorted element, or move a boundary after completing its edge. Recognising the pattern is the start; explaining why no element is missed proves the implementation.

Try a 40-minute study session: 5 minutes restating the contract, 15 coding one example, 10 testing edge cases and 10 explaining the trace and complexity. This is a study suggestion, not a Zoho test duration.

For guided general coding practice, Coding for Placements is an optional next step. Now reproduce the merge result without looking, and explain why it contains three occurrences of 3.