Zig-Zag String Problem Statement

Given a string 'STR' of length 'N' and an integer 'M' representing the number of rows in the zig-zag pattern, your task is to return the string formed by concatenating all 'M' rows when 'STR' is arranged in a zig-zag pattern.

Input:

The first line contains an integer 'T' representing the number of test cases.
Each test case begins with two integers 'N' and 'M', representing the length of the string 'STR' and the number of rows, respectively.
The next line contains the string 'STR'.

Output:

For each test case, return the concatenated string from the zig-zag pattern of 'STR' over 'M' rows.

Example:

Input:
N = 12, M = 3, STR = 'CODINGNINJAS'
Output:
'CNNOIGIJSDNA'
Explanation:

In a zig-zag pattern with 3 rows, row 1 is 'CNN', row 2 is 'OIGIJS', and row 3 is 'DNA'. The concatenation is 'CNNOIGIJSDNA'.

Constraints:

  • 1 ≤ T ≤ 102
  • 1 ≤ N ≤ 103
  • 1 ≤ M ≤ N
  • The string 'STR' contains only uppercase letters ('A-Z').
AnswerBot
4mo

Given a string and number of rows, return the concatenated string formed by arranging the input string in a zig-zag pattern over the specified number of rows.

  • Iterate through the string and distribute ...read more

Help your peers!
Select
Add answer anonymously...

Oyo Rooms Software Developer interview questions & answers

A Software Developer was asked Q. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given the head of a singly linked list and two integers m and n, reverse the nod...read more
A Software Developer was asked Q2. Merge sort algorithm and divide array in the chunks
A Software Developer was asked Q3. Position of Right Most Set Bit Determine the position of the rightmost set bit i...read more
Oyo Rooms Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits