Recursion

Objectives

  1. Significance
  2. Flow of control
  3. Memory usage
  4. Interview FAQs

Recursion is a process by which a function or a method calls itself again and again. Analogy: Anything that can be done in for loop can be done by recursion

Iteration VS Recursion

CriteriaRecursionIteration
DefinitionRecursion is a process where a method calls itself repeatedly until a base condition is met.Iteration is a process by which a piece of code is repeatedly executed for a finite number of times or until a condition is met.
ApplicabilityIs the application for functions.Is applicable for loops.
Chunk of code can be applied to?Works well for smaller code size.Works well for larger code size.
Memory FootprintUtilizes more memory as each recursive call is pushed to the stackComparatively less memory is used.
MaintainabilityDifficult to debug and maintainEasier to debug and maintain
Type of errors expectedResults in stack overflow if the base condition is not specified or not reachedMay execute infinitely but will ultimately stop execution with any memory errors
Time-ComplexityTime complexity is very high.Time complexity is relatively on the lower side.





Structure : Any method that implements Recursion has two basic parts:

  1. Method call which can call itself i.e. recursive
  2. A precondition that will stop the recursion.

Note that a precondition is necessary for any recursive method as, if we do not break the recursion then it will keep on running infinitely and result in a stack overflow.

Syntax- The general syntax of recursion is as follows:

methodName (T parameters…)
{   
    if (precondition == true)
//precondition or base condition
    {
        return result;
    }
    return methodName (T parameters…);
 
        //recursive call
}

Associated Error – Stack Overflow Error In Recursion

We are aware that when any method or function is called, the state of the function is stored on the stack and is retrieved when the function returns. The stack is used for the recursive method as well.

But in the case of recursion, a problem might occur if we do not define the base condition or when the base condition is somehow not reached or executed. If this situation occurs then the stack overflow may arise.

Types of recursion

  1. Tail Recursion
  2. Head Recursion
  3. Tree recursion

Tail Recursion

When the call to the recursive method is the last statement executed inside the recursive method, it is called “Tail Recursion”.

In tail recursion, the recursive call statement is usually executed along with the return statement of the method.

methodName ( T parameters…){
{   
    if (base_condition == true)
    {
        return result;
    }
    return methodName (T parameters …)      //tail recursion
}

Head Recursion

Head recursion is any recursive approach that is not a tail recursion. So even general recursion is ahead recursion.

methodName (T parameters…){
    if (some_condition == true)
    {
        return methodName (T parameters…);
    }
    return result;
}

Tree Recursion

Has two recursive calls(Left/right sub-tree)

Problem-Solving Using Recursion

The basic idea behind using recursion is to express the bigger problem in terms of smaller problems. Also, we need to add one or more base conditions so that we can come out of recursion.

PAGE BREAK

FAQS

Q #1.How does Recursion work in Java?

Answer: In recursion, the recursive function calls itself repeatedly until a base condition is satisfied. The memory for the called function is pushed on to the stack at the top of the memory for the calling function. For each function call, a separate copy of local variables is made.

Q #2) Why is Recursion used?

Answer: Recursion is used to solve those problems that can be broken down into smaller ones and the entire problem can be expressed in terms of a smaller problem.

Recursion is also used for those problems that are too complex to be solved using an iterative approach. Besides the problems for which time complexity is not an issue, use recursion.

Q #3) What are the benefits of Recursion?

Answer:

The benefits of Recursion include:

  1. Recursion reduces redundant calling of function.
  2. Recursion allows us to solve problems easily when compared to the iterative approach.

Q #4) Which one is better – Recursion or Iteration?

Answer: Recursion makes repeated calls until the base function is reached. Thus there is a memory overhead as a memory for each function call is pushed on to the stack.

Iteration on the other hand does not have much memory overhead. Recursion execution is slower than the iterative approach. Recursion reduces the size of the code while the iterative approach makes the code large.

Recursion is better than the iterative approach for problems like the Tower of Hanoi, tree traversals, etc.

Finding the factorial of a number using recursion
Factorial Recursive call

Question : how unflods while same method called:

https://leetcode.com/problems/merge-two-binary-trees/editorial/

DSA

Index

  • Theory
  • FAQs

A data structure is a way of organizing(defining, storing & retrieving) the data so that the data can be used efficiently.

You must about the data-structure before implementing in application to implement in a better and optimized way.

Note: This section is for

  1. Theoretical comparison
  2. Implementation of standard DS structures

(For Problem & Solution refer Competitive Programming section)

Topics :

  1. Introduction (& Brief Comparison)
    • Array vs Array-list
  2. Array & String
    1. Array: Way to declare
    2. Array: ways to copy
    3. Array: Advantages/Applications and disadvantages
  3. Stack[todo]
  4. Queue[todo]
  5. List & Linked-list
    1. Define the Structure and add element
    2. traversal
    3. Find loop
    4. reverse link-list
    5. Mid element
    6. Doubly Linked-list
  6. Tree
    1. Structure
    2. BST -Adding nodes
    3. Traversal
      1. DFS
        • In-order
        • Pre-Order
        • Post-Order
      2. BFS
  7. Map


List : Array Vs Linked List

Parameters of comparison :

  1. Speed of accessibility
    1. read/search
    2. insert/Delete – if tied to shifting other elements ,
  2. storage space required
ParameterArrayLinked List
StrategyStatic in nature (shortage or wastage of memory)dynamic in nature (grow and shrink at runtime as per need)
Access &TraversalFaster as index basedSlower as required traversal to the element,node by node
Insertion & RemovalRequire shifting (if middle element
is removed or added at middle)
Fastest Insertion and Deletion
Unit of storageUses dynamically allocated node to store data.
DefinitionCollection of Nodes

Frequently asked DS Question Solutions and Tips for optimization.

  1. Arrays Questions
    • Equilibrium Index of an array
    • Find row number of a binary matrix having maximum number of 1s
  2. Tree
    • Recursion
      1. Head recursion
      2. Tail recursion
      3. Tree recursion
  3. Utilities
    1. Occurrence of each element
      • Using additional DS i.e Map
      • Without using additional DS
    2. Max Length Sub-array
      • brute-force
      • hash-map
    3. convert character to upper case
    4. Matrix
      • properties of matrix
      • related to rows and columns
    5. Bit-wise Operation
      • Single iteration find non-duplicate(XOR)

MFA questions

K8s Certifications

  • Kubernetes and Cloud Native Associate (KCNA)
    • The Kubernetes and Cloud Native Associate (KCNA) exam demonstrates a user’s foundational knowledge and skills in Kubernetes and the wider cloud native ecosystem.A certified KCNA will confirm conceptual knowledge of the entire cloud native ecosystem, particularly focusing on Kubernetes.
  • Certified Kubernetes Application Developer (CKAD)
    • The Certified Kubernetes Application Developer exam certifies that users can design, build, configure, and expose cloud native applications for Kubernetes. A CKAD can define application resources and use core primitives to build, monitor, and troubleshoot scalable applications and tools in Kubernetes.
  • Certified Kubernetes Administrator (CKA)
    • The Certified Kubernetes Administrator (CKA) program provides assurance that CKAs have the skills, knowledge, and competency to perform the responsibilities of Kubernetes administrators. A certified Kubernetes administrator has demonstrated the ability to do basic installation as well as configuring and managing production-grade Kubernetes clusters.
    • Certified Kubernetes Security Specialist (CKS)
      • The Certified Kubernetes Security Specialist program provides assurance that the holder is comfortable and competent with a broad range of best practices. CKS certification covers skills for securing container-based applications and Kubernetes platforms during build, deployment and runtime. Candidates for CKS must hold a current Certified Kubernetes Administrator (CKA) certification to demonstrate they possess sufficient Kubernetes expertise before sitting for the CKS.

reference – https://kubernetes.io/training/

Secure coding standards

Secure coding standards

  1. OWASP – Open Web Application Security Project
  2. CWE – Common Weakness Enumeration
  3. SEI CERT –

Tool Used – Fortify (HP- security vulnerability code analyser )

OAuth

𝗔 𝗦𝘂𝗺𝗺𝗮𝗿𝘆 𝗼𝗳 𝗢𝗔𝘂𝘁𝗵 𝗮𝗻𝗱 𝗜𝘁𝘀 𝗨𝘀𝗲𝘀 🛡🔥

OAuth is an open protocol that enables secure authorization from web, mobile, or desktop applications. It’s used to grant access to services such as APIs and websites without needing to exchange passwords.

🔐 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗢𝗔𝘂𝘁𝗵?
OAuth stands for Open Authorization. It’s an open protocol that enables secure authorization from web, mobile, or desktop applications. It allows users to share their private resources stored on one site with another site without having to give away their credentials such as username and password.

✍ 𝘍𝘰𝘳 𝘦𝘹𝘢𝘮𝘱𝘭𝘦, 𝘪𝘧 𝘺𝘰𝘶 𝘩𝘢𝘷𝘦 𝘢 𝘎𝘰𝘰𝘨𝘭𝘦 𝘢𝘤𝘤𝘰𝘶𝘯𝘵 𝘢𝘯𝘥 𝘸𝘢𝘯𝘵 𝘵𝘰 𝘢𝘤𝘤𝘦𝘴𝘴 𝘠𝘰𝘶𝘛𝘶𝘣𝘦 𝘰𝘳 𝘎𝘮𝘢𝘪𝘭 𝘴𝘦𝘳𝘷𝘪𝘤𝘦𝘴, 𝘺𝘰𝘶 𝘤𝘢𝘯 𝘶𝘴𝘦 𝘖𝘈𝘶𝘵𝘩 𝘪𝘯𝘴𝘵𝘦𝘢𝘥 𝘰𝘧 𝘱𝘳𝘰𝘷𝘪𝘥𝘪𝘯𝘨 𝘺𝘰𝘶𝘳 𝘎𝘰𝘰𝘨𝘭𝘦 𝘢𝘤𝘤𝘰𝘶𝘯𝘵 𝘤𝘳𝘦𝘥𝘦𝘯𝘵𝘪𝘢𝘭𝘴.

🔐 𝗛𝗼𝘄 𝗗𝗼𝗲𝘀 𝗢𝗔𝘂𝘁𝗵 𝗪𝗼𝗿𝗸?
OAuth works by authenticating the user’s identity without them having to provide their login details directly. The process involves the user being redirected to the service provider where they are asked to approve the application that requests access. Once approved, the service provider sends back an access token which can then be used by the application to make authorized requests on behalf of the user.