Skip to content
Katabench
Try free

Zigzag Conversion

Medium About 15 min Free, no account needed

Write the string in a zigzag over n rows, then read row by row.

The brief

Write s in a zigzag pattern over numRows rows (down the rows, then diagonally back up, repeating), and return the characters read row by row.

"PAYPALISHIRING" over 3 rows:

Text
P   A   H   N
A P L S I I G
Y   I   R

reads "PAHNAPLSIIGYIR".

The hidden trap

numRows == 1 has no zigzag at all, naive direction-flipping code divides by zero or bounces out of range there. The hidden tests cover one row and more rows than characters.

Starter code

C#
using System;

public class Solution
{
    // TODO: return the characters of the zigzag pattern, read row by row.
    public string ConvertZigzag(string s, int numRows)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

String processing
Parse, scan and match text character by character with correct edge cases.

Related katas