Skip to content

Instantly share code, notes, and snippets.

@seahyc
Last active October 10, 2023 19:35
Show Gist options
  • Save seahyc/2a197e8c1c272739dba35c0b99998ef8 to your computer and use it in GitHub Desktop.
Save seahyc/2a197e8c1c272739dba35c0b99998ef8 to your computer and use it in GitHub Desktop.
Glints Technical Test

1. Factorials

You are given an integer N. Print the factorial of N, as defined below.

N! = N * (N-1) * (N-2) * ... * 3 * 2 * 1

Input

Input is a single integer N, where 1 <= N <= 100.

Output

Print the factorial of N.

Example

For an input N = 25, output will be 15511210043330985984000000.

2. Sorting

Given an array with n elements, sort this array in ascending order using only one of the following operations.

  1. Swap two elements.
  2. Reverse one sub-segment.

Input Format

The first line contains a single integer, n, which indicates the size of the array.

The next line contains n integers separated by spaces.

n

d1 d2 ... dn

​Constraints

  • ​2 <= n <= 100,000​
  • 0 <= di <= 1,000,000
  • All di are distinct.

Output Format

  1. If the array is already sorted, output yes on the first line. You do not need to output anything else.

  2. If you can sort this array using one single operation (from the two permitted operations) then output yes on the first line and then:

    a. If you can sort the array by swapping dl and dr, output swap l r in the second line. l and r are the indices of the elements to be swapped, assuming that the array is indexed from 1 to n.

    b. Else if it is possible to sort the array by reversing the segment d[l...r], output reverse l r in the second line. l and r are the indices of the first and last elements of the subsequence to be reversed, assuming that the array is indexed from 1 to n.

    d[l...r] represents the sub-sequence of the array, beginning at index l and r ending at index, both inclusive.

    If an array can be sorted by either swapping or reversing, stick to the swap-based method.

  3. If you cannot sort the array in either of the above ways, output no in the first line.

Sample Input #1

2

4 2

Sample Output #1

yes
swap 1 2

Sample Input #2

3 3 1 2

Sample Output #2

no

Sample Input #3

6

1 5 4 3 2 6

Sample Output #3

yes

reverse 2 5

Explanation

For #1, you can both swap(1, 2) and reverse(1, 2), but if you can sort the array using swap, output swap only.

For #2, it is impossible to sort by one single operation (among those permitted).

For #3, you can reverse the sub-array d[2...5] = "5 4 3 2", then the array becomes sorted.

3. Matrix Rotation

You are given a 2D matrix, a, of dimension MxN and a positive integer R. You have to rotate the matrix R times and print the resultant matrix. Rotation should be in anti-clockwise direction.

Rotation of a 4x5 matrix is represented by the following figure. Note that in one rotation, you have to shift elements by one step only (refer sample tests for more clarity).

It is guaranteed that the minimum of M and N will be even.

Input Format

First line contains three space separated integers, M, N and R, where M is the number of rows, N is number of columns in matrix, and R is the number of times the matrix has to be rotated. Then M lines follow, where each line contains N space separated positive integers. These M lines represent the matrix.

Constraints

  • 2 <= M, N <= 300
  • 1 <= R <= 109
  • min(M, N) % 2 == 0
  • 1 <= aij <= 108, where i ∈ [1..M] & j ∈ [1..N]

Output Format

Print the rotated matrix.

Sample Input #00

4 4 1

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Sample Output #00

2 3 4 8

1 7 11 12

5 6 10 16

9 13 14 15

Sample Input #01

4 4 2

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Sample Output #01

3 4 8 12

2 11 10 16

1 7 6 15

5 9 13 14

Sample Input #02

5 4 7

1 2 3 4

7 8 9 10

13 14 15 16

19 20 21 22

25 26 27 28

Sample Output #02

28 27 26 25

22 9 15 19

16 8 21 13

10 14 20 7

4 3 2 1

Sample Input #03

2 2 3

1 1

1 1

Sample Output #03

1 1

1 1

Explanation

Sample Case #00: Here is an illustration of what happens when the matrix is rotated once.

  1  2   3  4      2  3  4  8

  5  6   7  8      1  7 11 12

 9  10 11 12 -> 5  6 10 16

13 14 15 16     9 13 14 15

Sample Case #01: Here is what happens when to the matrix after two rotations.

 1  2  3  4          2  3  4  8          3  4  8 12

 5  6  7   8        1  7  11 12         2 11 10 16

 9 10 11 12  ->  5 6 10 16  ->    1  7  6 15

13 14 15 16      9 13 14 15        5  9 13 14

Sample Case #02: Following are the intermediate states.

1  2  3  4              2  3  4 10         3  4 10 16       4 10 16 22

7  8  9 10             1  9 15 16        2 15 21 22       3 21 20 28

13 14 15 16   ->   7 8 21 22   ->   1 9 20 28   ->   2 15 14 27 ->

19 20 21 22        13 14 20 28      7 8 14 27         1 9 8 26

25 26 27 28        19 25 26 27      13 19 25 26     7 13 19 25

 

10 16 22 28     16 22 28 27      22 28 27 26         28 27 26 25

4  20 14 27      10 14  8 26       16  8  9 25            22  9 15 19

3 21  8 26   ->   4 20 9 25   ->   10 14 15 19   ->   16  8 21 13

2  15 9 25         3 21 15 19        4 20 21 13           10 14 20  7

1  7 13 19         2  1  7 13          3  2  1  7               4  3  2  1

Sample Case #03: As all elements are same, any rotation will reflect the same matrix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment