Q's Flashcards

1
Q

Using relevant examples, distinguish between linear and non-linear data structures

A

Linear data structures are arranged sequentially and linearly whereas non-linear data structure is where data is not arranged sequentially and linearly.
Linear-array, queue, linked list, etc.
non-linear- Trees and graphs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

You are climbing a staircase. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer

A
class stairs {
    // A simple recursive program to find
    // n'th fibonacci number
    static int fib(int n)
    {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
    // Returns number of ways to reach s'th stair
    static int countWays(int s)
    {
        return fib(s + 1);
    }
    /* Driver program to test above function */
    public static void main(String args[])
    {
        int s = 4;
        System.out.println("Number of ways = " + countWays(s));
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given a chess board having NN cells, we need to place ܰN queens in such a way that no
queen is attacked by any other queen. A queen can attack horizontally, vertically and diagonally.
So initially we are having ܰN
N unattacked cells where we need to place ܰ queens. Let’s place the first queen at a cell (i,j), so now the number of unattacked cells is reduced, and number of queens to be placed is ܰN-1. Place the next queen at some unattacked cell.
This again reduces the number of unattacked cells and number of queens to be placed becomes ܰN-2. Continue doing this, as long as following conditions hold.
The number of unattacked cells is not 0. The number of queens to be placed is not 0. If the
number of queens to be placed becomes 0, then it’s over, we found a solution. But if the number
of unattacked cells become 0, then we need to backtrack, i.e. remove the last placed queen
from its current cell and place it at some other cell. We do this recursively.

A

Check https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/ for solution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Given an array of integers and of size n, find the sum of its elements. For example, if the array arr = {1,2,3}, so return 6 Complete the simpleArraySum function below. It must return the sum of the array elements as an integer. Write a complete C++ /Java / Python
program to find the sum of the array. The function prototype is provided below, your program should instantiate an array and print out the final sum.

A

static int simpleArraySum(int[] ar) {

      int sum = 0;
    for(int i=0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly