Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

Infosys – Power Programmer | Specialist – Coding Test

Infosys – Power Programmer – Coding Question

Program:

Chess master has chess board & 2 piece he forgot, Something like this. They provide some matrix and code function which takes 8 integer input parameters. We need to complete that function and meet below output. Find below test cases:

 

Test 1: 1,1,1,1,2,2,2,2 = 1  

Test 2: 1,1,1,1,1,1,2,2 = 0

Test 3: 0,1,3,4,5,6,7,8 = 8

 

Explanation for above test cases:

Test 1: Here if user enter 1,1,1,1,2,2,2,2 this input then output should be 1

Test 2: Here if user enter 1,1,1,1,1,1,2,2 this input then output should be 0

Test 3: Here if user enter 0,1,3,4,5,6,7,8 this input then output should be 8

 

Program Understanding:

If the function takes 8 integers, maybe it's positions of 4 pieces on a chessboard:
e.g., x1, y1, x2, y2, x3, y3, x4, y4

 

Test 1:

Input: 1,1,1,1,2,2,2,2
Let’s group them as 4 pieces:

  • Piece A: (1,1)
  • Piece B: (1,1)
  • Piece C: (2,2)
  • Piece D: (2,2)

So, we have 2 unique positions: (1,1) and (2,2)

Output: 1 — This suggests that it’s valid even if multiple pieces share a position — maybe they form valid pairs?


Test 2:

Input: 1,1,1,1,1,1,2,2

Grouped:

  • A: (1,1)
  • B: (1,1)
  • C: (1,1)
  • D: (2,2)

Now we have three at (1,1) and one at (2,2) — maybe that’s invalid.

Output: 0 — Seems to break a “pairing” rule.

 

Test 3:

Input: 0,1,3,4,5,6,7,8

Grouped:

·       A: (0,1)

·       B: (3,4)

·       C: (5,6)

·       D: (7,8)

 

Here, all 4 positions are unique.

Output: 8 — maybe it's based on number of unique positions * 2?

(4 unique positions * 2 = 8)


Case

Unique Positions

Repeats

Output

1

2

Each 2 times

1

2

2

3 same

0

3

4

All unique

8

 

If multiple pieces are on the same square, maybe it's invalid or valid depending on the rules.

 

So, the logic could be:

  • If positions grouped into 2 exact pairs → return 1
  • If positions are all unique → return 8
  • Else → return 0

 

When write function - Select C# programming language – which handles all 3 cases: 



static void Main()
{
    Console.WriteLine(AnalyzeChessPositions(1, 1, 1, 1, 2, 2, 2, 2));
    //Console.WriteLine(AnalyzeChessPositions(1, 1, 1, 1, 1, 1, 2, 2));
    //Console.WriteLine(AnalyzeChessPositions(0, 1, 3, 4, 5, 6, 7, 8));          
}

public static int AnalyzeChessPositions(int x1, int y1, int x2, int y2,
                                        int x3, int y3, int x4, int y4)
{
    var positions = new List&ltstring> 
      {
          $"{x1},{y1}",
          $"{x2},{y2}",
          $"{x3},{y3}",
          $"{x4},{y4}"
      };

    var groups = positions.GroupBy(p => p).ToList();

    // Case 1: Two positions each appearing twice
    if (groups.Count == 2 && groups.All(g => g.Count() == 2))
        return 1;

    // Case 2: All four positions unique
    if (groups.Count == 4)
        return 8;

    // Any other case
    return 0;
}

    

                       

Post a Comment

0 Comments