Frontend

Zig-Zag Grid Rendering (Google Frontend Interview) | Platform Round

A classic platform-round problem asked at Google where you build a dynamic zig-zag numbered grid using HTML, CSS, and JavaScript. Given an input n, generate an n × n grid where numbers alternate direction in each row. This tests DOM manipulation, logical thinking, and clean UI rendering under real interview constraints.

ByteAndBites·Jul 12, 2026
Zig-Zag Grid Rendering (Google Frontend Interview) | Platform Round

In this blog, we will explore a classic problem often encountered in platform-round interviews, particularly at Google. The task is to dynamically create a zig-zag numbered grid using HTML, CSS, and JavaScript. Given an input n, we will generate an n × n grid where numbers alternate direction in each row. This problem not only tests your DOM manipulation skills but also your logical thinking and ability to create a clean UI under real interview constraints.

Breaking Down the Problem

When we are asked to create a zig-zag grid for a specific integer input n, we can think of it as constructing a two-dimensional grid. The numbers in this grid will be filled sequentially but will alternate direction for each row:
  • For example, if n = 5, the grid will look like this:
12345
109876
1112131415
2019181716
2122232425


Approach

To tackle this problem, we can follow these steps:

  1. Initialize the grid: Create a two-dimensional array to hold the numbers.
  2. Loop through the rows: For each row, determine the direction of filling (left to right or right to left).
  3. Fill the grid: Based on the current direction, fill the row with numbers, updating the counter accordingly.
  4. Render the grid dynamically: Use JavaScript to create HTML elements and display the grid on the web page.


Solution

Below is a solution implemented in HTML, CSS, and JavaScript:

HTML(index.html)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Zig Zag Grid</title>
  <link rel="stylesheet" href="./index.css" />
</head>
<body>

  <div class="container">
    <h2>Zig-Zag Grid</h2>

    <div class="controls">
      <input type="number" id="sizeInput" placeholder="Enter n" />
      <button onclick="generateGrid()">Generate</button>
    </div>

    <div id="grid" class="grid"></div>
  </div>

  <script src="./index.js"></script>
</body>
</html>

CSS(index.css)
body {
    font-family: Arial, sans-serif;
    background: #f7f7f7;
    display: flex;
    justify-content: center;
    margin-top: 40px;
  }
  
  .container {
    text-align: center;
  }
  
  .controls {
    margin-bottom: 20px;
  }
  
  input {
    padding: 8px;
    width: 100px;
    margin-right: 10px;
  }
  
  button {
    padding: 8px 12px;
    cursor: pointer;
  }
  
  .grid {
    display: grid;
    gap: 6px;
    justify-content: center;
  }
  
  .cell {
    width: 50px;
    height: 50px;
    background: white;
    border: 1px solid #ddd;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
  }

JavaScript(index.js)
function generateGrid() {
    const n = parseInt(document.getElementById("sizeInput").value);
    const grid = document.getElementById("grid");
  
    // Clear previous grid
    grid.innerHTML = "";
  
    if (!n || n <= 0) return;
  
    // Set grid columns dynamically
    grid.style.gridTemplateColumns = `repeat(${n}, 50px)`;
  
    let num = 1;
  
    for (let row = 0; row < n; row++) {
      let rowValues = [];
  
      // Fill row normally
      for (let col = 0; col < n; col++) {
        rowValues.push(num++);
      }
  
      // Reverse every alternate row (zig-zag)
      if (row % 2 === 1) {
        rowValues.reverse();
      }
  
      // Render row
      for (let value of rowValues) {
        const cell = document.createElement("div");
        cell.className = "cell";
        cell.innerText = value;
        grid.appendChild(cell);
      }
    }
  }

How It Works

  1. HTML: Sets up the basic structure with an input for the size of the grid, a button to generate the grid, and a div to hold the grid cells.
  2. CSS: Styles the grid, inputs, and buttons for a clean layout.
  3. JavaScript: Handles the logic for generating the zig-zag grid based on the inputted size. It fills the grid in the specified pattern and renders it in the designated div.

You can copy and paste each section into its own respective file (HTML, CSS, JavaScript) and then open the HTML file in your browser to see the zig-zag grid in action!


Blog image


Conclusion

Creating a zig-zag grid is an excellent exercise for frontend developers preparing for interviews. This problem allows you to showcase your problem-solving skills and your ability to manipulate the DOM efficiently. By following the structured approach outlined above, you can develop a clean solution and become more confident in tackling similar problems in the future. Remember, practice makes perfect!


InterviewGoogleJavascriptHTMLUI
Frontend Interview Playbook 🚀
Series
Frontend Interview Playbook 🚀
View series
A curated series of real frontend interview questions from top companies like Google, Amazon, Uber, Atlassian, Roku, etc. Each blog breaks down problems, thought processes, and optimal solutions to help you master concepts, improve problem-solving, and confidently crack frontend interviews at leading tech companies.