FSR170823 :- DSA Questions

Q.s 1:- What is a Data structure & algorithm?
Q.s 2:- Explain Dynamic and weak typing
Q.s 3 :- Explain ADT ?
Q.s 4 :- Give an example of array method such as includes, sort, some & join.
Q.s 5 :- Write a Implementation of Stack in JavaScript

  1. DSA - Data structure refers to the organization , storage and retrieval of data and algorithm is the set of instructions used to solve a problem .

  2. Javascript is a dynamic language . variable in js are not directly associated with any particular type of values and can be reassigned to values of all type .
    Js is also a weakly typed language, because it allows implicit conversions.
    example:
    var a=" RamaJyothi";
    a=20;

  3. ADT is a type of object ,It is called “abstract” because it gives an implementation-independent view.
    So, in orderto simplify the process of solving problems, we can create data
    structures along with their operations, and such data structures that are not inbuilt are known as Abstract Data Type (ADT).

let arr=[1,5,6,7];
let res=arr.includes(5);
let arr1=[11,14,12,15]
let res1=arr1.sort();
let res2=arr1.some(items=>items>14)
let arr2=["this", "is", "commmuntiy"]
let res3=arr2.join("-");


class Stack{
constructor(){
this.items=[]
this.length=0;
}
this.add=function(element){
this.items.push(element);
this.length+=1;
}
this.del=function(){
if(this.length==0){
console.log("stack is empty")

}
this.length--;
 return this.items.pop();

}
this.peek=function(){
if(this.length==0){
console.log("stack is empty")
}
  console.log(this.items[this.length-1]);
  }
    }
 read(){
        console.log(this.items,this.length);

    }
  
}

let x= new Stack();
x.add('11');
x.add('12');
x.add('john');
x.add('data');
x.add(true);
x.add(false);
x,read();
x.del();
x.peek();

1 Like

1.data structure :It is a way to organize data that enables it to be processed in a efficient time.
Algorithm :set of instructions for solving a problem or accomplishing a task.

2.Dynamic and weak typing : JavaScript is a dynamic language with dynamic types, variables in JS are not directly associated with any particular value type and any variable can be assigned.
JS is a weakly typed language, which means it allows implicit type conversion.

3.ADT(Abstract Data Type):It is a type of object or type for objects whose behaviour is defined by a set of values and a set of operations.

1 Like

1)data structure is a way to organize data to processed in efficient time.
Algorithm is a set of instruction for solving problem.
2) dynamic type, javascript variables are not directly associated with particular value type.
weakly type, which allows implicit type conversion when the operation have mismatched types and java script allows weakly typed language.
3)Abstract Data Type , in this objects whose behaviour is defined by a group of values and a group of operations

1 Like

What is a Data Structure and Algorithm?
Data Structure is a way to organize data that enables it to be processed in an efficient time.
An algorithm is a set of instructions for solving a problem or completing a task.

Explain Dynamic and Weak Typing.
Ans: Variables in JavaScript can be dynamically assigned values. It is not assigned with any data type for a variable.
example:

let a ='Shashank';
a=98;

Explain ADT.
Ans: Abstract Data Type is used to create our own data to implement an easy approach to a solution.

Give an Example of an array method such as includes, sort, some & join.
Ans:

//includes
let arr =[1,2,3,4,5,6];
let res1 = arr.includes(3);
let res2 = arr.includes(10);
console.log(`Does Array ${arr} includes 3? : ${res1}`);
console.log(`Does Array ${arr} includes 10? : ${res2}`);

//Sort
arr.sort((a, b) => a - b);
console.log(`Ascending order Sorting sort() ${arr}`);
arr.sort((a, b) => b - a);
console.log(`Descending order Sorting sort() ${arr}`);

//some
let res3 = arr.some(item => item > 8);
console.log(`Is any element in ${arr} is greater than 8?: ${res3}`);

//join
let arr2 =['Hello','myself','Shashank'];
let res4 = arr2.join(' ');
console.log(`Array after joining ${arr2} is: ${res4} `)

Write an Implementation of Stack in JavaScript?
Ans:

class Stack {
    constructor() {
        this.stack= []
        this.size= 0;
        this.add = function (x) {
            this.stack.push(x);
            this.size+= 1;
        }
        this.del = function () {
            if (this.size== 0) {
                console.log('Stack is Empty');
            }
     else{
            this.size--;
            return this.stack.pop();
          }
        }
        this.top= function () {
            if (this.size== 0) {
                console.log('Stack is empty');
            }
            console.log(this.stack[this.size- 1]);
        }
    }
    read() {
        console.log(this.stack);

    }
}
let a = new Stack();
a.add(1);
a.add(2);
a.add(3);
a.add(4);
a.add(5);
a.read();
a.top();
a.del();
a.read();
a.top();
1 Like

1.data structure:- it is a way to organize data that enables it to be processed in an efficient time
algorithm:-an algorithm is a set of instructions for solving a problem or accomplishing a task
2.java script is a dynamic language with dynamic types. variables in java script are not directly associated with ant particular value type and any variable can be assigned values of all types.
3.abstract data type is a type for objects whose behavior is defined by a set of values and set of operation.

1 Like

Qs.4
Sort

let arr = [8,6,5,1,2,4,7,3]

let result = arr.sort()

console.log('Original array',arr);
console.log('New return array',result);

Includes

let arra1 = [ 2,5,7,4,6,1]

let res = arra1.includes(4);
let res2 = arra1.includes(9);

console.log(arra1)
console.log(res)
console.log(res2)

some

let arra = [1,6,78,4,5,1]
let result0 = arra.some(item => item > 8)
let result3 = arra.some(item=> item %3 ==0)
console.log(result0)
console.log(result3)

join

 let arr7 = ['S','W','A','P','N','I','L']
 let result5 = arr7.join()
 let result6 = arr7.join("")

console.log(arr7)
console.log(result5)
console.log(result6)

1 Like

answer -5

//impliment
class Stack {
    constructor() {
        this.items = [];
    }
    
    // add element to the stack
    add(element) {
        return this.items.push(element);
    }
    
 //remove
    remove() {
        if(this.items.length > 0) {
            return this.items.pop();
        }
    }
    
    // view the last element
    peek() {
        return this.items[this.items.length - 1];
    }
    
    //  empty
    isEmpty(){
       return this.items.length == 0;
    }
   
    // the size of the stack
    size(){
        return this.items.length;
    }
 
    // empty the stack
    clear(){
        this.items = [];
    }
}

let stack = new Stack();
stack.add(1);
stack.add(2);
stack.add(4);
stack.add(8);
console.log(stack.items);

stack.remove();
console.log(stack.items);

console.log(stack.peek());

console.log(stack.isEmpty());

console.log(stack.size());

stack.clear();
console.log(stack.items);
1 Like

4.Array.includes
let arr =[2,5,7,4,6,1]
let result=arr.includes(4)
let result2=arr.includes(9)
console.log(arr)
console.log(result)
console.log(result2)

1 Like

Implementation of Stack in JavaScript

class Stack{
    constructor(){
       this.items = []; 
       this.length = 0;
    
        this.add = function(x){
            this.items.push(x);
            this.length += 1;
        }
 
    this.del = function(){
        if(this.length==0){
            return -1;
        }
        this.length--
        return this.items.pop();
    } 

    this.peek = function(){
        if(this.length == 0){
            console.log(-1);
        }
        console.log(this.items[this.length-1]);
    }  

    }

    read(){
        console.log(this.items,this.length);
    }
}

let x = new Stack();
x.add("nobita");
x.add("doremon");
x.add(25);
x.add(20);
x.add(true);
x.add(false);
x.add("bheem");
x.del();
x.peek();
x.read();
1 Like

4.array.includes:
let arr=[1,2,3,4];
let res=arr.includes(3);
console.log(arr)

array.sort:
let arr=[1,2,3,4,5]
let result=arr.sort()
console.log(arr)

array.some:
let arr=[1,2,3,4,5]
let result=arr.some(item=>item>2)
console.log(arr)

array.join:
let arr=[“She”,“is”,“beautiful”];
let res=arr.join(‘.’);
console.log(arr)

1 Like

answer 4

join

const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
console.log(elements.join(''));
console.log(elements.join('-'));

sort

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);

some

const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0;
console.log(array.some(even));

includes

const array1 = [1, 2, 3];
console.log(array1.includes(2));
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
console.log(pets.includes('at'));
1 Like
  1. Data structure can be arranged in logical or mathematical arrangement of data is known as DSA.
    Algorithm means it is a sequence of steps performed on the data using efficient data structure to solve a given problem.

2.Dynamic typing means variables in javascript are not associated with any particular type.
weak typing means it allows implicit type conversions when an operation involves.

3.ADT is a type or class for objects whose behaviour is defined by a set of values and set of operations.it is called “abstract” because it gives an implementation independent view.

1 Like

Explain ADT

Abstract Data type (ADT) is a type/class for objects whose behavior is defined by a set of values and a set of operations.

1 Like

4)let arr1 = [1,2,3,4,5]
let res= arr1.includes(4)
console.log(res)

let arr2 = [45,67,12,89,23]
let res1= arr2.sort((a , b) => a-b)
console.log(res1)

let res2=arr2.some(item => 90)
console.log(res2)

let arr3=[‘a’, ‘b’, ‘c’ , ‘d’]
let result= arr3.join(“-”)
console.log(result)

1 Like
let arr4 = [2,5,7,4,6,1]
let result3 = arr.includes(4)
console.log(arr4)
console.log(result3)
console.log(result2)

5.class Stack{
    constructor(){
       this.items = []; 
       this.length = 0;
    
        this.add = function(x){
            this.items.push(x);
            this.length += 1;
        }
 
    this.del = function(){
        if(this.length==0){
            return -1;
       }
    this.peek = function(){
        if(this.length == 0){
            console.log(-1);
        }
        console.log(this.items[this.length-1]);
    }  

    }

    read(){
        console.log(this.items,this.length);
    }
}

let x = new Stack();
x.add("nobita");
x.add("doremon");
x.add(25);
x.add(20);
x.add(true);
x.add(false);
x.add("bheem");
x.del();
x.peek();
x.read();
1 Like

5.Stack:

class Stack{
constructor(){
this.items=[];
this.length=0;

    this.add=function(a){
        this.items.push(a);
        this.length+=1;
    }
   
    this.del=function(){
        if(this.length==0){
            console.log(-1)
        }
        this.length--;
        return this.items.pop();
    }
    
    this.peek=function(){
        if(this.length==0){
            console.log(-1);
        }
        console.log(this.items[this.length-1]);
    }
}
read(){
    console.log(this.items,this.length);
}

}
let x=new Stack();
x.add(11);
x.add(12);
x.add(“jin”);
x.add(“tae”);
x.add(true);
x.add(false);
x.del();
x.peek();
x.read();

1 Like

1.DATA STRUCTURE : It is organize data that enable to be processed in an efficient time.
ALGORITHM: It is a set of instruction for solving problems

2.DYNAMIC AND WEAK TYPING: JavaScript is a dynamic language and it is not directly associated with any value, and variable can be assigned .JavaScript is a weakly typed language

3.ADT-Abstract Data Type: It is a type of Object and its behavior is defined by set of values ,and a set of Operations.

4.let arr1=[3,2,6,8,4,1,10,7]
let result = arr1.sort()

console.log(“new array”,result);

INCLUDES

let arr2=[4,3,6,12,8,7]
let res1= arr2.includes(6);
let res2=arr2.includes(1);

console.log(res1);
console.log(res2);

SOME

let arr3=[6,3,9,2,8,5,10]
let res3 = arr3.some(item => item > 8)
let res4 = arr3.some(item=> item %5 ==0)
console.log(res3)
console.log(res4)

//JOIN
let arr4 = [‘HELLO’, ‘HOW’,‘ARE’,‘YOU’]
let res5 = arr4.join(’ ')

console.log(res5);

class Stack{
constructor(){
this.ele = [];
this.size= 0;
this.add= function(element){
this.ele.push(element);
this.size +=1;

    } 
    this.delete=function(){
        if(this.size == 0){
            console.log(-1)
        }
        this.size--
        return this.ele.pop();
    }  
    this.peek = function(){
        if(this.size == 0){
            console.log(-1);
        }
        console.log(this.ele[this.size-1]);
    }
}
read(){
    console.log(this.ele,this.size);
}

}

let k=new Stack();
k.add(11);
k.add(12);
k.add(13);
k.add(14);
k.add(“hani”);
k.add(“true”);
k.add(“false”);
k.add(“a”);
k.del();
k.del();
k.peek();
k.read()

Q1.–> Data structure is location that can be used to store and organizing data in different manner and algorithm is collection of instruction for solving problems or accomplished task.

Q2.–> dynamic typing languages, values rather than variables have types. java script is a dynamic typed variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types.

weak typing lang has looser typing rules does not require the explicit specification of different types of variables. and weak typing is may produce unpredictable result at runtime.

Q3–> ADT is a type or class for object whose behavior is defined by set of values and set of operations.
ADT only mention what operation will be perform not how to be implement.

Q4–>
1)sort :
let arr1=[1,7,8,4,2,5,1,3]
console.log(‘orignal array’,arr1);
let result1=arr1.sort()
console.log(‘sorted array’,result1);
2) includes:
let arr2 = [3,9,2,5,7,4,6,1]

let result2= arr2.includes(6)

let result3 = arr2.includes(8)

console.log(arr2)

console.log(result2) // true
console.log(result3) // false
3) join:
let arr3= [‘The’, ‘good’, ‘things’, ‘happen’, ‘ever’]
let result4= arr3.join()
result5= arr1.join(-)
console.log(arr3) //[‘The’, ‘good’, ‘things’, ‘happen’, ‘ever’]
console.log(result4) // “The good things happen ever”
console.log(result5) //“The-good-things-happen-ever”

4)some:
let arr4= [2,5,7,4,6,1,8]
let result6 =arr4.some(item => item > 9)
let result7 = arr4.some(item => item % 4== 0)
console.log(arr4)
console.log(result6) // false
console.log(result7) // true