0% found this document useful (0 votes)
5 views2 pages

Recursive Functions and Algorithms

The document presents four programming challenges: finding the nth Fibonacci number using recursion, checking if a string is a palindrome with recursion, rotating an array in-place, and reversing a singly linked list iteratively. Each challenge includes a brief explanation and multiple test cases to illustrate expected outputs. The focus is on implementing solutions using specified methods.

Uploaded by

srikarpatel9177
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Recursive Functions and Algorithms

The document presents four programming challenges: finding the nth Fibonacci number using recursion, checking if a string is a palindrome with recursion, rotating an array in-place, and reversing a singly linked list iteratively. Each challenge includes a brief explanation and multiple test cases to illustrate expected outputs. The focus is on implementing solutions using specified methods.

Uploaded by

srikarpatel9177
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Recursion

1: Finding the nth Fibonacci Number

Question: Write a function to find the nth Fibonacci number. The Fibonacci sequence starts with 0
and 1, and each subsequent number is the sum of the two preceding numbers. Implement the
function using recursion.

Test Cases:

1. Input: n = 5 Output: 5 (Explanation: The 5th Fibonacci number is 5, as the sequence is 0, 1, 1,


2, 3, 5...)

2. Input: n = 10 Output: 55 (Explanation: The 10th Fibonacci number is 55)

3. Input: n = 0 Output: 0 (Explanation: The 0th Fibonacci number is 0)

2: Checking for Palindromic Strings

Question: Write a function to determine if a given string is a palindrome. A palindrome is a string


that reads the same forwards and backwards, ignoring spaces, punctuation, and capitalization.
Implement the function using recursion.

Test Cases:

1. Input: "racecar" Output: True (Explanation: "racecar" reads the same forwards and
backwards)

2. Input: "A man, a plan, a canal, Panama!" Output: True (Explanation: Ignoring spaces,
punctuation, and capitalization, the string is a palindrome)

3. Input: "hello" Output: False (Explanation: "hello" does not read the same forwards and
backwards)

3: Rotating an Array

Question: Given an array of integers nums and an integer k, rotate the array to the right by k steps.
Implement the solution using an in-place algorithm.

Test Cases:

1. Input: nums = [1, 2, 3, 4, 5], k = 2 Output: [4, 5, 1, 2, 3] (Explanation: After rotating the array
to the right by 2 steps, it becomes [4, 5, 1, 2, 3])

2. Input: nums = [3, 7, -2, 5], k = 3 Output: [7, -2, 5, 3] (Explanation: After rotating the array to
the right by 3 steps, it becomes [7, -2, 5, 3])

3. Input: nums = [-1, -100, 3, 99], k = 1 Output: [99, -1, -100, 3] (Explanation: After rotating the
array to the right by 1 step, it becomes [99, -1, -100, 3])
4: Reversing a Linked List

Question: Given a singly linked list, reverse it in-place and return the head of the reversed list.
Implement the solution using iterative approach.

Test Cases:

1. Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output: 5 -> 4 -> 3 -> 2 -> 1 -> NULL

2. Input: 3 -> 7 -> 11 -> 2 -> NULL Output: 2 -> 11 -> 7 -> 3 -> NULL

3. Input: 8 -> NULL Output: 8 -> NULL

You might also like