Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Grid Challenge Problem Solution

YASH PAL, 31 July 202424 January 2026

In this HackerRank Grid challenge problem solution, we have given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not.

The grid is illustrated below.

a b c
a d e
e f g

The rows are already in alphabetical order. The columns a a e, b d f and c e g are also in alphabetical order, so the answer would be YES. Only elements within the same row can be rearranged. They cannot be moved to a different row.

Function Description

Complete the gridChallenge function in the editor below.

gridChallenge has the following parameter(s):

  • string grid[n]: an array of strings
HackerRank Grid challenge problem solution

HackerRank Grid challenge problem solution in Python.

def checkLex(arr, n):
    for j in range(n-1):
        for k in range(n):
            if arr[j][k] > arr[j+1][k]:
                return False
    return True
    

t = int(input().strip())

for i in range(t):
    n = int(input().strip())
    line = []
    for j in range(n):
        x = list(input().strip())
        x.sort()
        line.append(x)
    
    if checkLex(line, n):
        print("YES")
    else:
        print("NO")

Grid challenge problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
String[]grid = new String[n];
for(int i=0;i<n;i++)
grid[i] = sort(br.readLine());

boolean ok = true;

for(int i=0;i<n;i++){
for(int j=1;j<n;j++){
if(grid[j].charAt(i) < grid[j-1].charAt(i)){
ok = false;
break;
}
}
}
System.out.println(ok?"YES":"NO");

}
}

public static String sort(String s){
char[] array = s.toCharArray();
Arrays.sort(array);
return new String(array);
}
}

Problem solution in C++.

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

string s[111];

int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> s[i], sort(s[i].begin(), s[i].end());
bool flag = true;
for (int i = 0; i < n; i++) for (int j = 0; j + 1 < n; j++) if (s[j][i] > s[j + 1][i]) flag = false;
puts(flag ? "YES" : "NO");
}
return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int num_testcases, number;
    char array[100][100];
    int i, j, k, l;
    char c, t;
    int match;
    
    scanf("%d", &num_testcases);
    
    for (i = 0; i < num_testcases; i++) {
        scanf("%d", &number);
        match = 1;
        for (j = 0; j < number; j++) { /* no of lines */
            while ((c = fgetc(stdin)) == 'n') continue;
            ungetc(c, stdin);
            for (k = 0; ((k < number) && ((c = fgetc(stdin)) != 'n')); k++) {
                for(l = k; l>0; l--) {
                    if (array[j][l-1] > c) {
                        array[j][l] = array[j][l-1];
                    } else {
                        break;
                    }
                }
                array[j][l] = c;
          //      printf("wrote %c into [%d][%d]n", c, j, l);
            }
        }
        /*printf("Array read is:n");
        for (l = 0; l < number; l++) {
            for (j = 0; j < number; j++) {
                printf("%c", array[l][j]);
            }
            printf("n");
        }
        printf("n");
        */
        for (l = 0; l < number-1; l++) {
            for (j = 0; j < number-1; j++) {
                if ((array[l][j] > array[l][j+1]) ||
                    (array[l][j] > array[l+1][j])) {
                    match = 0;
                    goto done;
                }
            }
        }
        /* Check last row and last column */
        for (k = 0; k < number-1; k++) {
            if (array[l][k] > array[l][k+1]) {
                match = 0;
                goto done;
            }
        }
        for (k = 0; k < number-1; k++) {
            if (array[k][j] > array[k+1][j]) {
                match = 0;
                goto done;
            }
        }
done:
        if (match) {
            printf("YESn");
        } else {
            printf("NOn");
        }
    }
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes