Validity of a given Tic-Tac-Toe board configuration
Last Updated :
23 Jul, 2025
A Tic-Tac-Toe board is given after some moves are played. Find out if the given board is valid, i.e., is it possible to reach this board position after some moves or not.
Note that every arbitrary filled grid of 9 spaces isn't valid e.g. a grid filled with 3 X and 6 O isn't valid situation because each player needs to take alternate turns.

Input is given as a 1D array of size 9.
Examples:
Input: board[] = {'X', 'X', 'O',
'O', 'O', 'X',
'X', 'O', 'X'};
Output: Valid
Input: board[] = {'O', 'X', 'X',
'O', 'X', 'X',
'O', 'O', 'X'};
Output: Invalid
(Both X and O cannot win)
Input: board[] = {'O', 'X', ' ',
' ', ' ', ' ',
' ', ' ', ' '};
Output: Valid
(Valid board with only two moves played)
Basically, to find the validity of an input grid, we can think of the conditions when an input grid is invalid. Let no. of "X"s be countX and no. of "O"s be countO. Since we know that the game starts with X, a given grid of Tic-Tac-Toe game would be definitely invalid if following two conditions meet
- countX != countO AND
- countX != countO + 1
- Since "X" is always the first move, second condition is also required.
- Now does it mean that all the remaining board positions are valid one? The answer is NO. Think of the cases when input grid is such that both X and O are making straight lines. This is also not
- valid position because the game ends when one player wins. So we need to check the following condition as well
- If input grid shows that both the players are in winning situation, it's an invalid position.
- If input grid shows that the player with O has put a straight-line (i.e. is in win condition) and countX != countO, it's an invalid position. The reason is that O plays his move only after X plays his
- move. Since X has started the game, O would win when both X and O has played equal no. of moves.
- If input grid shows that X is in winning condition than xCount must be one greater that oCount.
- Armed with above conditions i.e. a), b), c) and d), we can now easily formulate an algorithm/program to check the validity of a given Tic-Tac-Toe board position.
1) countX == countO or countX == countO + 1
2) If O is in win condition then check
a) If X also wins, not valid
b) If xbox != obox , not valid
3) If X is in win condition then check if xCount is
one more than oCount or not
Another way to find the validity of a given board is using 'inverse method' i.e. rule out all the possibilities when a given board is invalid.
C++
// C++ program to check whether a given tic tac toe
// board is valid or not
#include <iostream>
using namespace std;
// This matrix is used to find indexes to check all
// possible winning triplets in board[0..8]
int win[8][3] = {{0, 1, 2}, // Check first row.
{3, 4, 5}, // Check second Row
{6, 7, 8}, // Check third Row
{0, 3, 6}, // Check first column
{1, 4, 7}, // Check second Column
{2, 5, 8}, // Check third Column
{0, 4, 8}, // Check first Diagonal
{2, 4, 6}}; // Check second Diagonal
// Returns true if character 'c' wins. c can be either
// 'X' or 'O'
bool isCWin(char *board, char c)
{
// Check all possible winning combinations
for (int i=0; i<8; i++)
if (board[win[i][0]] == c &&
board[win[i][1]] == c &&
board[win[i][2]] == c )
return true;
return false;
}
// Returns true if given board is valid, else returns false
bool isValid(char board[9])
{
// Count number of 'X' and 'O' in the given board
int xCount=0, oCount=0;
for (int i=0; i<9; i++)
{
if (board[i]=='X') xCount++;
if (board[i]=='O') oCount++;
}
// Board can be valid only if either xCount and oCount
// is same or count is one more than oCount
if (xCount==oCount || xCount==oCount+1)
{
// Check if 'O' is winner
if (isCWin(board, 'O'))
{
// Check if 'X' is also winner, then
// return false
if (isCWin(board, 'X'))
return false;
// Else return true xCount and yCount are same
return (xCount == oCount);
}
// If 'X' wins, then count of X must be greater
if (isCWin(board, 'X') && xCount != oCount + 1)
return false;
// If 'O' is not winner, then return true
return true;
}
return false;
}
// Driver program
int main()
{
char board[] = {'X', 'X', 'O',
'O', 'O', 'X',
'X', 'O', 'X'};
(isValid(board))? cout << "Given board is valid":
cout << "Given board is not valid";
return 0;
}
Java
// Java program to check whether a given tic tac toe
// board is valid or not
import java.io.*;
class GFG {
// This matrix is used to find indexes to check all
// possible winning triplets in board[0..8]
static int win[][] = {{0, 1, 2}, // Check first row.
{3, 4, 5}, // Check second Row
{6, 7, 8}, // Check third Row
{0, 3, 6}, // Check first column
{1, 4, 7}, // Check second Column
{2, 5, 8}, // Check third Column
{0, 4, 8}, // Check first Diagonal
{2, 4, 6}}; // Check second Diagonal
// Returns true if character 'c' wins. c can be either
// 'X' or 'O'
static boolean isCWin(char[] board, char c) {
// Check all possible winning combinations
for (int i = 0; i < 8; i++) {
if (board[win[i][0]] == c
&& board[win[i][1]] == c
&& board[win[i][2]] == c) {
return true;
}
}
return false;
}
// Returns true if given board is valid, else returns false
static boolean isValid(char board[]) {
// Count number of 'X' and 'O' in the given board
int xCount = 0, oCount = 0;
for (int i = 0; i < 9; i++) {
if (board[i] == 'X') {
xCount++;
}
if (board[i] == 'O') {
oCount++;
}
}
// Board can be valid only if either xCount and oCount
// is same or count is one more than oCount
if (xCount == oCount || xCount == oCount + 1) {
// Check if 'O' is winner
if (isCWin(board, 'O')) {
// Check if 'X' is also winner, then
// return false
if (isCWin(board, 'X')) {
return false;
}
// Else return true xCount and yCount are same
return (xCount == oCount);
}
// If 'X' wins, then count of X must be greater
if (isCWin(board, 'X') && xCount != oCount + 1) {
return false;
}
// If 'O' is not winner, then return true
return true;
}
return false;
}
// Driver program
public static void main(String[] args) {
char board[] = {'X', 'X', 'O', 'O', 'O', 'X', 'X', 'O', 'X'};
if ((isValid(board))) {
System.out.println("Given board is valid");
} else {
System.out.println("Given board is not valid");
}
}
}
//this code contributed by PrinciRaj1992
Python3
# Python3 program to check whether a given tic tac toe
# board is valid or not
# Returns true if char wins. Char can be either
# 'X' or 'O'
def win_check(arr, char):
# Check all possible winning combinations
matches = [[0, 1, 2], [3, 4, 5],
[6, 7, 8], [0, 3, 6],
[1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]]
for i in range(8):
if(arr[(matches[i][0])] == char and
arr[(matches[i][1])] == char and
arr[(matches[i][2])] == char):
return True
return False
def is_valid(arr):
# Count number of 'X' and 'O' in the given board
xcount = arr.count('X')
ocount = arr.count('O')
# Board can be valid only if either xcount and ocount
# is same or count is one more than oCount
if(xcount == ocount+1 or xcount == ocount):
# Check if O wins
if win_check(arr, 'O'):
# Check if X wins, At a given point only one can win,
# if X also wins then return Invalid
if win_check(arr, 'X'):
return "Invalid"
# O can only win if xcount == ocount in case where whole
# board has values in each position.
if xcount == ocount:
return "Valid"
# If X wins then it should be xc == oc + 1,
# If not return Invalid
if win_check(arr, 'X') and xcount != ocount+1:
return "Invalid"
# if O is not the winner return Valid
if not win_check(arr, 'O'):
return "valid"
# If nothing above matches return invalid
return "Invalid"
# Driver Code
arr = ['X', 'X', 'O',
'O', 'O', 'X',
'X', 'O', 'X']
print("Given board is " + is_valid(arr))
C#
// C# program to check whether a given
// tic tac toe board is valid or not
using System;
class GFG
{
// This matrix is used to find indexes
// to check all possible winning triplets
// in board[0..8]
public static int[][] win = new int[][]
{
new int[] {0, 1, 2},
new int[] {3, 4, 5},
new int[] {6, 7, 8},
new int[] {0, 3, 6},
new int[] {1, 4, 7},
new int[] {2, 5, 8},
new int[] {0, 4, 8},
new int[] {2, 4, 6}
};
// Returns true if character 'c'
// wins. c can be either 'X' or 'O'
public static bool isCWin(char[] board,
char c)
{
// Check all possible winning
// combinations
for (int i = 0; i < 8; i++)
{
if (board[win[i][0]] == c &&
board[win[i][1]] == c &&
board[win[i][2]] == c)
{
return true;
}
}
return false;
}
// Returns true if given board
// is valid, else returns false
public static bool isValid(char[] board)
{
// Count number of 'X' and
// 'O' in the given board
int xCount = 0, oCount = 0;
for (int i = 0; i < 9; i++)
{
if (board[i] == 'X')
{
xCount++;
}
if (board[i] == 'O')
{
oCount++;
}
}
// Board can be valid only if either
// xCount and oCount is same or count
// is one more than oCount
if (xCount == oCount ||
xCount == oCount + 1)
{
// Check if 'O' is winner
if (isCWin(board, 'O'))
{
// Check if 'X' is also winner,
// then return false
if (isCWin(board, 'X'))
{
return false;
}
// Else return true xCount
// and yCount are same
return (xCount == oCount);
}
// If 'X' wins, then count of
// X must be greater
if (isCWin(board, 'X') &&
xCount != oCount + 1)
{
return false;
}
// If 'O' is not winner,
// then return true
return true;
}
return false;
}
// Driver Code
public static void Main(string[] args)
{
char[] board = new char[] {'X', 'X', 'O', 'O', 'O',
'X', 'X', 'O', 'X'};
if ((isValid(board)))
{
Console.WriteLine("Given board is valid");
}
else
{
Console.WriteLine("Given board is not valid");
}
}
}
// This code is contributed by Shrikant13
JavaScript
<script>
// Javascript program to check whether a given
// tic tac toe board is valid or not
// This matrix is used to find indexes
// to check all possible winning triplets
// in board[0..8]
// Returns true if character 'c' wins.
// c can be either 'X' or 'O'
function isCWin(board, c)
{
let win = new Array(new Array(0, 1, 2), // Check first row.
new Array(3, 4, 5), // Check second Row
new Array(6, 7, 8), // Check third Row
new Array(0, 3, 6), // Check first column
new Array(1, 4, 7), // Check second Column
new Array(2, 5, 8), // Check third Column
new Array(0, 4, 8), // Check first Diagonal
new Array(2, 4, 6)); // Check second Diagonal
// Check all possible winning combinations
for (let i = 0; i < 8; i++)
if (board[win[i][0]] == c &&
board[win[i][1]] == c &&
board[win[i][2]] == c )
return true;
return false;
}
// Returns true if given board is
// valid, else returns false
function isValid(board)
{
// Count number of 'X' and 'O'
// in the given board
let xCount = 0;
let oCount = 0;
for (let i = 0; i < 9; i++)
{
if (board[i] == 'X') xCount++;
if (board[i] == 'O') oCount++;
}
// Board can be valid only if either
// xCount and oCount is same or count
// is one more than oCount
if (xCount == oCount || xCount == oCount + 1)
{
// Check if 'O' is winner
if (isCWin(board, 'O'))
{
// Check if 'X' is also winner,
// then return false
if (isCWin(board, 'X'))
return false;
// Else return true xCount and
// yCount are same
return (xCount == oCount);
}
// If 'X' wins, then count of X
// must be greater
if (isCWin(board, 'X') &&
xCount != oCount + 1)
return false;
// If 'O' is not winner, then
// return true
return true;
}
return false;
}
// Driver Code
let board = new Array('X', 'X', 'O','O',
'O', 'X','X', 'O', 'X');
if(isValid(board))
document.write("Given board is valid");
else
document.write("Given board is not valid");
// This code is contributed
// by Saurabh Jaiswal
</script>
PHP
<?php
// PHP program to check whether a given
// tic tac toe board is valid or not
// This matrix is used to find indexes
// to check all possible winning triplets
// in board[0..8]
// Returns true if character 'c' wins.
// c can be either 'X' or 'O'
function isCWin($board, $c)
{
$win = array(array(0, 1, 2), // Check first row.
array(3, 4, 5), // Check second Row
array(6, 7, 8), // Check third Row
array(0, 3, 6), // Check first column
array(1, 4, 7), // Check second Column
array(2, 5, 8), // Check third Column
array(0, 4, 8), // Check first Diagonal
array(2, 4, 6)); // Check second Diagonal
// Check all possible winning combinations
for ($i = 0; $i < 8; $i++)
if ($board[$win[$i][0]] == $c &&
$board[$win[$i][1]] == $c &&
$board[$win[$i][2]] == $c )
return true;
return false;
}
// Returns true if given board is
// valid, else returns false
function isValid(&$board)
{
// Count number of 'X' and 'O'
// in the given board
$xCount = 0;
$oCount = 0;
for ($i = 0; $i < 9; $i++)
{
if ($board[$i] == 'X') $xCount++;
if ($board[$i] == 'O') $oCount++;
}
// Board can be valid only if either
// xCount and oCount is same or count
// is one more than oCount
if ($xCount == $oCount || $xCount == $oCount + 1)
{
// Check if 'O' is winner
if (isCWin($board, 'O'))
{
// Check if 'X' is also winner,
// then return false
if (isCWin($board, 'X'))
return false;
// Else return true xCount and
// yCount are same
return ($xCount == $oCount);
}
// If 'X' wins, then count of X
// must be greater
if (isCWin($board, 'X') &&
$xCount != $oCount + 1)
return false;
// If 'O' is not winner, then
// return true
return true;
}
return false;
}
// Driver Code
$board = array('X', 'X', 'O','O',
'O', 'X','X', 'O', 'X');
if(isValid($board))
echo("Given board is valid");
else
echo ("Given board is not valid");
// This code is contributed
// by Shivi_Aggarwal
?>
OutputGiven board is valid
Time complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Approach 2:
The algorithm to check if a Tic-Tac-Toe board is valid or not is as follows:
- Initialize a 2D array win of size 8x3, which contains all possible winning combinations in Tic-Tac-Toe. Each row of the win array represents a winning combination, and each element in a row represents a cell index on the board.
- Define a function isCWin(board, c) which takes a board configuration board and a character c ('X' or 'O') as inputs, and returns true if character c has won on the board.
- Inside the isCWin function, iterate over each row of the win array. Check if the board has the same character c at all three cell indices of the current row. If yes, return true, as the character c has won.
- Define a function isValid(board) which takes a board configuration board as input, and returns true if the board is valid, else returns false.
- Inside the isValid function, count the number of 'X' and 'O' characters on the board, and store them in xCount and oCount variables, respectively.
- The board can be valid only if either xCount and oCount are the same, or xCount is one more than oCount.
- If 'O' is a winner on the board, check if 'X' is also a winner. If yes, return false as both 'X' and 'O' cannot win at the same time. If not, return true if xCount and oCount are the same, else return false.
- If 'X' is a winner on the board, then xCount must be one more than oCount. If not, return false.
- If 'O' is not a winner, return true as the board is valid.
Here is the code of the above approach:
C++
// Returns true if character 'c' wins. c can be either
// 'X' or 'O'
#include<bits/stdc++.h>
using namespace std;
bool isWinner(char *board, char c)
{
// Check all possible winning combinations
if ((board[0] == c && board[1] == c && board[2] == c) ||
(board[3] == c && board[4] == c && board[5] == c) ||
(board[6] == c && board[7] == c && board[8] == c) ||
(board[0] == c && board[3] == c && board[6] == c) ||
(board[1] == c && board[4] == c && board[7] == c) ||
(board[2] == c && board[5] == c && board[8] == c) ||
(board[0] == c && board[4] == c && board[8] == c) ||
(board[2] == c && board[4] == c && board[6] == c))
return true;
return false;
}
// Returns true if given board is valid, else returns false
bool isValid(char board[9])
{
// Count number of 'X' and 'O' in the given board
int xCount=0, oCount=0;
for (int i=0; i<9; i++)
{
if (board[i]=='X') xCount++;
if (board[i]=='O') oCount++;
}
// Board can be valid only if either xCount and oCount
// is same or count is one more than oCount
if (xCount==oCount || xCount==oCount+1)
{
// Check if there is only one winner
if (isWinner(board, 'X') && isWinner(board, 'O'))
return false;
// If 'X' wins, then count of X must be greater
if (isWinner(board, 'X') && xCount != oCount + 1)
return false;
// If 'O' wins, then count of X must be same as oCount
if (isWinner(board, 'O') && xCount != oCount)
return false;
return true;
}
return false;
}
// Driver program
int main()
{
char board[] = {'X', 'X', 'O',
'O', 'O', 'X',
'X', 'O', 'X'};
(isValid(board))? cout << "Given board is valid":
cout << "Given board is not valid";
return 0;
}
Java
import java.util.Arrays;
public class TicTacToe {
// Returns true if character 'c' wins. c can be either 'X' or 'O'
public static boolean isWinner(char[] board, char c) {
// Check all possible winning combinations
if ((board[0] == c && board[1] == c && board[2] == c) ||
(board[3] == c && board[4] == c && board[5] == c) ||
(board[6] == c && board[7] == c && board[8] == c) ||
(board[0] == c && board[3] == c && board[6] == c) ||
(board[1] == c && board[4] == c && board[7] == c) ||
(board[2] == c && board[5] == c && board[8] == c) ||
(board[0] == c && board[4] == c && board[8] == c) ||
(board[2] == c && board[4] == c && board[6] == c))
return true;
return false;
}
// Returns true if given board is valid, else returns false
public static boolean isValid(char[] board) {
// Count number of 'X' and 'O' in the given board
int xCount = 0, oCount = 0;
for (int i = 0; i < 9; i++) {
if (board[i] == 'X')
xCount++;
if (board[i] == 'O')
oCount++;
}
// Board can be valid only if either xCount and oCount is same or count is one more than oCount
if (xCount == oCount || xCount == oCount + 1) {
// Check if there is only one winner
if (isWinner(board, 'X') && isWinner(board, 'O'))
return false;
// If 'X' wins, then count of X must be greater
if (isWinner(board, 'X') && xCount != oCount + 1)
return false;
// If 'O' wins, then count of X must be same as oCount
if (isWinner(board, 'O') && xCount != oCount)
return false;
return true;
}
return false;
}
// Driver program
public static void main(String[] args) {
char[] board = {'X', 'X', 'O',
'O', 'O', 'X',
'X', 'O', 'X'};
if (isValid(board))
System.out.println("Given board is valid");
else
System.out.println("Given board is not valid");
}
}
Python3
# Python Program for the above approach
def isWinner(board, c):
# Check all possible winning combinations
if (board[0] == c and board[1] == c and board[2] == c) or \
(board[3] == c and board[4] == c and board[5] == c) or \
(board[6] == c and board[7] == c and board[8] == c) or \
(board[0] == c and board[3] == c and board[6] == c) or \
(board[1] == c and board[4] == c and board[7] == c) or \
(board[2] == c and board[5] == c and board[8] == c) or \
(board[0] == c and board[4] == c and board[8] == c) or \
(board[2] == c and board[4] == c and board[6] == c):
return True
return False
def isValid(board):
# Count number of 'X' and 'O' in the given board
xCount = 0
oCount = 0
for i in range(9):
if board[i] == 'X':
xCount += 1
if board[i] == 'O':
oCount += 1
# Board can be valid only if either xCount and oCount
# is same or count is one more than oCount
if xCount == oCount or xCount == oCount + 1:
# Check if there is only one winner
if isWinner(board, 'X') and isWinner(board, 'O'):
return False
# If 'X' wins, then count of X must be greater
if isWinner(board, 'X') and xCount != oCount + 1:
return False
# If 'O' wins, then count of X must be same as oCount
if isWinner(board, 'O') and xCount != oCount:
return False
return True
return False
# Driver program
board = ['X', 'X', 'O',
'O', 'O', 'X',
'X', 'O', 'X']
if isValid(board):
print("Given board is valid")
else:
print("Given board is not valid")
# THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL
C#
using System;
public class TicTacToe {
// Returns true if character 'c' wins. c can be either 'X' or 'O'
public static bool IsWinner(char[] board, char c) {
// Check all possible winning combinations
if ((board[0] == c && board[1] == c && board[2] == c) ||
(board[3] == c && board[4] == c && board[5] == c) ||
(board[6] == c && board[7] == c && board[8] == c) ||
(board[0] == c && board[3] == c && board[6] == c) ||
(board[1] == c && board[4] == c && board[7] == c) ||
(board[2] == c && board[5] == c && board[8] == c) ||
(board[0] == c && board[4] == c && board[8] == c) ||
(board[2] == c && board[4] == c && board[6] == c))
return true;
return false;
}
// Returns true if given board is valid, else returns false
public static bool IsValid(char[] board) {
// Count number of 'X' and 'O' in the given board
int xCount = 0, oCount = 0;
for (int i = 0; i < 9; i++) {
if (board[i] == 'X')
xCount++;
if (board[i] == 'O')
oCount++;
}
// Board can be valid only if either xCount and oCount is same or count is one more than oCount
if (xCount == oCount || xCount == oCount + 1) {
// Check if there is only one winner
if (IsWinner(board, 'X') && IsWinner(board, 'O'))
return false;
// If 'X' wins, then count of X must be greater
if (IsWinner(board, 'X') && xCount != oCount + 1)
return false;
// If 'O' wins, then count of X must be same as oCount
if (IsWinner(board, 'O') && xCount != oCount)
return false;
return true;
}
return false;
}
// Driver program
public static void Main(string[] args) {
char[] board = { 'X', 'X', 'O',
'O', 'O', 'X',
'X', 'O', 'X' };
if (IsValid(board))
Console.WriteLine("Given board is valid");
else
Console.WriteLine("Given board is not valid");
}
}
JavaScript
// Returns true if character 'c' wins. c can be either 'X' or 'O'
function isWinner(board, c) {
// Check all possible winning combinations
if (
(board[0] === c && board[1] === c && board[2] === c) ||
(board[3] === c && board[4] === c && board[5] === c) ||
(board[6] === c && board[7] === c && board[8] === c) ||
(board[0] === c && board[3] === c && board[6] === c) ||
(board[1] === c && board[4] === c && board[7] === c) ||
(board[2] === c && board[5] === c && board[8] === c) ||
(board[0] === c && board[4] === c && board[8] === c) ||
(board[2] === c && board[4] === c && board[6] === c)
) {
return true;
}
return false;
}
// Returns true if given board is valid, else returns false
function isValid(board) {
// Count number of 'X' and 'O' in the given board
let xCount = 0;
let oCount = 0;
for (let i = 0; i < 9; i++) {
if (board[i] === 'X') xCount++;
if (board[i] === 'O') oCount++;
}
// Board can be valid only if either xCount and oCount
// is same or count is one more than oCount
if (xCount == oCount || xCount == oCount + 1) {
// Check if there is only one winner
if (isWinner(board, 'X') && isWinner(board, 'O')) {
return false;
}
// If 'X' wins, then count of X must be greater
if (isWinner(board, 'X') && xCount !== oCount + 1) {
return false;
}
// If 'O' wins, then count of X must be same as oCount
if (isWinner(board, 'O') && xCount !== oCount) {
return false;
}
return true;
}
return false;
}
// Driver program
const board = ['X', 'X', 'O', 'O', 'O', 'X', 'X', 'O', 'X'];
isValid(board) ? console.log('Given board is valid') : console.log('Given board is not valid');
OutputGiven board is valid
Time complexity: O(N^2)
Auxiliary Space: O(N)
Thanks to Utkarsh for suggesting this solution. This article is contributed by Aarti_Rathi and Utkarsh.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem