> For the complete documentation index, see [llms.txt](https://sandeepamaranath.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sandeepamaranath.gitbook.io/notes/data-structures/practice-dsa.md).

# Practice DSA

{% hint style="info" %}
This page is collection of all the resources I followed in order to prep for the DSA
{% endhint %}

## Recursion

{% embed url="<https://www.youtube.com/watch?ab_channel=freeCodeCamp.org&t=890s&v=IJDJ0kBx2LM>" %}

### 1. Print the string but using recursion&#x20;

```javascript
function printRecursive(str) {

     if(str.length === 0){
          return ""
     }

     return str.charAt(0)  + printRecursive(str.substring(1))    
}

printRecursive("Sandeep")

```

### 2. Reverse a string

```javascript
function reverseStr(str) {

     if(str.length === 0){
          return ""
     }

     return reverseStr(str.substring(1)) +  str.charAt(0)    
}

reverseStr("Sandeep")

//const sub = str.substring(1) -> gives string from index mentioned to end


// p
// F("p")p    + e
// F(ep)   pe  + e
// F(eep)   pee + d
// F(deep) peed  + n
// F(ndeep) peedn  + a
// F(andeep)peedna + S

// peednas
```

### 3. String Palindrome

```javascript
function isPal(str) {

    if(str.length===0 || str.length===1)return true


    if(str[0] === (str[str.length-1])){
        return isPal(str.substring(1,str.length-1))
    }
 
    return false
}


isPal("loocl")
// isPal("malayalal")
```

### 3. Decimal to Binary -> Example Convert 25 = 1 1 1 0 1 0 0 1

```javascript
function decToBin(num, bin) {

  if(num === 0){
      return bin
  }

  bin += num % 2
  return decToBin(Math.floor(num/2), bin)

}

decToBin(5,"")
```

### 4. Sum of n input numbers

```javascript
function sumOfNums(num){
    if(num === 1) {
        return 1
    }

    return num + sumOfNums(num-1)
    
}

sumOfNums(5)

/*

1              -> 1
2  + F(sum(1)) -> 2 + 1 = 3
3  + F(sum(2)) -> 3 + 3 = 6
4  + F(sum(3))
5  + F(sum(4))
6  + F(sum(5))
7  + F(sum(6))
9  + F(sum(8))
10 + F(sum(9))

*/ 
```

### 5. Fibonacci series -> recursion

```
function fib(number) {

    if(number === 0 || number === 1) return number

    return fib(number - 1) + fib(number - 2)
}


fib(6)

//f(0 1 2 3 4 5 6)

// 0 1 1 2 3 5 8

/*

fib(0) = 0
fib(1) = 1

fib(2) = fib(0) + fib(1)

fib(4) = fib(3)           +      fib(2)
       = fib(2) + fib(1)  +    fib(1) + fib(0)
       = fib(1) + fib(0)  + fib(1) + fib(1) +fib(0)
       = 1+0+1+1+0


**F(0) -> 0

*F(1) -> 1

*F(2) -> *F(1)=1 + **F(0)=0

*F(3) -> *F(2) + F(1)

F(4) -> *F(3) + F(2) 
*/ 
//
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sandeepamaranath.gitbook.io/notes/data-structures/practice-dsa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
