JavaScript Cheat Sheet that covers all the important concepts, syntax











Overview of Common Roles in .NET Projects

 

Role Responsibilities Tech Stack Relevance
  • Backend Developer
  • Write APIs, business logic, integrate with databases
  • All: .NET Framework, Core, and .NET 5/6/7/8
  • Frontend Developer
  • Build UI (e.g., Razor pages, Blazor, Angular/React)
  • .NET Core+, with Blazor or JS SPA frameworks
  • Full Stack Developer
  • Handles both backend and frontend
  • More common in .NET Core and .NET 5+
  • DevOps Engineer
  • CI/CD, deployment, cloud (Azure, Docker, Kubernetes)
  • Mostly .NET Core and .NET 5/6/7/8
  • Solution Architect
  • Designs system architecture
  • All versions
  • QA/Test Engineer
  • Writes unit/integration tests, automation scripts
  • All (Test tools differ slightly)
  • Database Developer
  • DB schema design, stored procedures, EF Core configuration
  • All, but EF Core used from .NET Core onward
  • Legacy Support Engineer
  • Maintain/upgrade older apps (e.g., ASP.NET Web Forms)
  • Mostly .NET Framework
  • Cloud Engineer
  • Cloud-first design (Azure Functions, App Services)
  • .NET Core and .NET 5/6/7+

What is Agile? Explain.

 Ans. 

Agile is a project management methodology that emphasizes flexibility, collaboration, and iterative development.

  • Agile focuses on delivering working software in short, iterative cycles called sprints.

  • It values customer collaboration, responding to change, and continuous improvement.

  • Scrum and Kanban are popular Agile frameworks used in software development.

  • Agile promotes self-organizing teams and regular feedback to adapt to changing requirements.

What are the methods to transfer data between components in Angular?

 Ans. 

Methods to transfer data between components in Angular include Input and Output properties, ViewChild, Services, and Event Emitters.

  • Using Input and Output properties to pass data from parent to child components and emit events from child to parent components.

  • Using ViewChild to access child components and their properties directly from the parent component.

  • Using Services to create a shared service that can be injected into multiple components to share data.

  • Using Event Emitters to emit custom events from child components that can be subscribed to in parent components.

Reversing an array in JavaScript can be done in several ways depending on the specific use case. Below, I’ll show all major types of examples

 

. Using the Built-in .reverse() Method

This is the simplest and most direct way.

javascript

let arr = [1, 2, 3, 4, 5]; arr.reverse(); console.log(arr); // [5, 4, 3, 2, 1]

⚠️ This method mutates the original array.


🔁 2. Using the Spread Operator & .reverse() (Non-mutating)

To avoid changing the original array:

javascript

let arr = [1, 2, 3, 4, 5]; let reversed = [...arr].reverse(); console.log(reversed); // [5, 4, 3, 2, 1] console.log(arr); // [1, 2, 3, 4, 5] — unchanged

🔁 3. Using a for Loop

Manual reverse using a classic for loop:

javascript

let arr = [1, 2, 3, 4, 5]; let reversed = []; for (let i = arr.length - 1; i >= 0; i--) { reversed.push(arr[i]); } console.log(reversed); // [5, 4, 3, 2, 1]

🔁 4. Using a while Loop

javascript

let arr = [1, 2, 3, 4, 5]; let reversed = []; let i = arr.length - 1; while (i >= 0) { reversed.push(arr[i]); i--; } console.log(reversed); // [5, 4, 3, 2, 1]

🔁 5. Using Recursion

A functional way to reverse an array:

javascript

function reverseArray(arr) { if (arr.length === 0) return []; return [arr.pop()].concat(reverseArray(arr)); } let arr = [1, 2, 3, 4, 5]; console.log(reverseArray([...arr])); // [5, 4, 3, 2, 1]

🔁 This also mutates the original if not cloned with [...arr].


🔁 6. Using reduceRight()

A more functional-style approach:

javascript

let arr = [1, 2, 3, 4, 5]; let reversed = arr.reduceRight((acc, curr) => { acc.push(curr); return acc; }, []); console.log(reversed); // [5, 4, 3, 2, 1]

🔁 7. Swapping Elements In-Place

This doesn't use extra space and is optimal for large arrays:

javascript

let arr = [1, 2, 3, 4, 5]; for (let i = 0; i < arr.length / 2; i++) { let temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } console.log(arr); // [5, 4, 3, 2, 1]

Summary Table:

MethodMutates?Extra SpaceNotes
.reverse()Simple, in-place
[...arr].reverse()Clones, safer
for/while loopGood for manual control
Recursion✅ or ❌Elegant but less efficient
reduceRight()Functional-style
In-place swapOptimal for performance


What is the difference between authorization and authentication?

 Ans. 

Authentication verifies the identity of a user, while authorization determines what a user can access.

  • Authentication confirms the user's identity through credentials like username and password.

  • Authorization controls the user's access to resources or features based on their authenticated identity.

  • Example: Logging into a website (authentication) and then being able to view/edit specific pages based on user roles (authorization).

To solve this problem, we need to reverse a singly linked list. The goal is to reverse the direction of the pointers in the list such that the last node becomes the first node, and the first node becomes the last node.

 

Approach

The approach involves iterating through the linked list while reversing the pointers of each node. Here’s a step-by-step breakdown:

  1. Initialization: Start with two pointers, prev and current. The prev pointer is initialized to null because the new tail (originally the first node) should point to null after reversal. The current pointer starts at the head of the list.

  2. Iteration: Traverse the list with the current pointer. For each node:

    • Store the next node: Temporarily store the next node of the current node to avoid losing the reference when we change the current node's pointer.

    • Reverse the pointer: Set the current node's next pointer to point to the prev node.

    • Move pointers forward: Update prev to be the current node and move current to the next node in the original list (which was stored earlier).

  3. Termination: Once current becomes null, the prev pointer will be at the new head of the reversed list. Return prev.

This approach efficiently reverses the list in a single pass through the nodes with constant space complexity, making it optimal.

Solution Code

javascript
class ListNode {
  constructor(val = 0, next = null) {
    this.val = val;
    this.next = next;
  }
}

function reverseList(head) {
  let prev = null;
  let current = head;
  
  while (current !== null) {
    const nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  
  return prev;
}

// Debug your code below
let head = new ListNode(1, new ListNode(2));
let reversedHead = reverseList(head);
console.log(reversedHead.val);
console.log(reversedHead.next.val);

Explanation

  • Initialization: The prev pointer starts as null because the first node of the original list will become the last node in the reversed list, pointing to null. The current pointer starts at the head of the list.

  • Loop Through List: For each node, we:

    1. Store the next node (nextTemp) to remember the rest of the list.

    2. Reverse the current node's pointer to point to prev.

    3. Move prev to the current node and current to the next node (nextTemp).

  • Termination: When current becomes nullprev will be pointing to the last node of the original list, which is now the head of the reversed list. We return prev as the new head.

To solve this problem, we need to calculate the success rate of posts that immediately follow a failed post for each user. The success rate is defined as the percentage of such posts that are successful

 

Approach

  1. Problem Analysis: The task involves analyzing the sequence of posts made by each user to determine the success rate of posts that come immediately after a failed post.

  2. Key Insight: For each user, we need to check the status of the previous post. If the previous post was unsuccessful, the current post is considered in the calculation of the success rate.

  3. Data Processing:

    • Ordering Posts: For each user, order their posts by date (and post ID to handle any ties in timestamps) to determine the sequence.

    • Previous Post Status: Use the LAG window function to retrieve the status of the previous post in the sequence.

    • Filter Relevant Posts: Select only those posts where the previous post was unsuccessful.

    • Calculate Success Rate: For the filtered posts, compute the percentage of successful posts per user.

  4. Output: Present the results with user IDs and their corresponding success rates, ordered by increasing success rate.

Solution Code

sql
WITH post_sequence AS (
    SELECT 
        user_id,
        is_successful_post,
        LAG(is_successful_post) OVER (
            PARTITION BY user_id 
            ORDER BY post_date, post_id
        ) AS prev_success
    FROM post
)
SELECT
    user_id,
    AVG(CASE WHEN is_successful_post THEN 100.0 ELSE 0.0 END) AS next_post_sc_rate
FROM post_sequence
WHERE prev_success = False
GROUP BY user_id
ORDER BY next_post_sc_rate;

Explanation

  1. CTE post_sequence:

    • Partitioning and Ordering: The posts are partitioned by user_id and ordered by post_date and post_id to ensure the sequence is correctly determined.

    • Previous Post Status: The LAG function retrieves the is_successful_post value of the previous post in the sequence for each user.

  2. Main Query:

    • Filtering Relevant Posts: Only posts where the previous post was unsuccessful (prev_success = False) are considered.

    • Success Rate Calculation: The AVG function combined with a CASE statement computes the percentage of successful posts. Successful posts contribute 100.0 to the average, while unsuccessful posts contribute 0.0.

    • Grouping and Ordering: Results are grouped by user_id and ordered by the computed success rate in ascending order.

To solve this problem, we need to identify the top customer (based on the highest number of orders) for each of the last 5 years present in the orders data.

 To solve this problem, we need to identify the top customer (based on the highest number of orders) for each of the last 5 years present in the orders data. The solution involves grouping orders by year and customer, counting the orders per customer per year, and then selecting the customer with the highest count for each year. In case of ties, the customer with the smallest customer_id is chosen.

Solution Code

sql
WITH last_five_years AS (
    SELECT DISTINCT EXTRACT(YEAR FROM order_date)::integer AS year
    FROM orders
    ORDER BY year DESC
    LIMIT 5
), 
yearly_orders AS (
    SELECT 
        EXTRACT(YEAR FROM o.order_date)::integer AS year,
        o.customer_id,
        COUNT(*) AS total_orders
    FROM orders o
    WHERE EXTRACT(YEAR FROM o.order_date)::integer IN (SELECT year FROM last_five_years)
    GROUP BY EXTRACT(YEAR FROM o.order_date), o.customer_id
), 
ranked_customers AS (
    SELECT 
        year,
        customer_id,
        total_orders,
        ROW_NUMBER() OVER (PARTITION BY year ORDER BY total_orders DESC, customer_id ASC) AS rn
    FROM yearly_orders
)
SELECT 
    rc.year,
    rc.customer_id,
    c.first_name,
    c.last_name,
    rc.total_orders
FROM ranked_customers rc
JOIN customers c ON rc.customer_id = c.customer_id
WHERE rc.rn = 1
ORDER BY rc.year DESC;

Explanation

  1. Identify Last 5 Years: The last_five_years CTE retrieves the 5 most recent distinct years from the orders table, ordered descendingly.

  2. Count Orders per Customer per Year: The yearly_orders CTE counts the number of orders for each customer in each of the last 5 years.

  3. Rank Customers by Order Count: The ranked_customers CTE assigns a rank to each customer within each year based on:

    • total_orders in descending order (highest first).

    • customer_id in ascending order (to break ties by selecting the smallest ID).

  4. Select Top Customers: The final query joins the top-ranked customers (where rank is 1) with the customers table to include their names and orders the result by year in descending order.

To solve this problem, we need to retrieve the top-earning employee from each department, ordered by department name.

 To solve this problem, we need to retrieve the top-earning employee from each department, ordered by department name. The solution involves using a window function to rank employees within each department by their salary and then selecting the highest earner from each group.


Approach

  1. Join Tables: Combine the employees and departments tables to get the department names associated with each employee.

  2. Rank Employees: Use the ROW_NUMBER() window function to rank employees within each department based on their salary in descending order. To handle ties (employees with the same salary), we break the tie by sorting on employee_id in ascending order.

  3. Filter Top Earners: Select only the employees ranked first in their respective departments.

  4. Order Results: Sort the final result by department name alphabetically.

Solution Code

sql
WITH ranked_employees AS (
    SELECT 
        d.name AS department_name,
        e.id AS employee_id,
        e.first_name,
        e.last_name,
        e.salary,
        ROW_NUMBER() OVER (
            PARTITION BY e.department_id 
            ORDER BY e.salary DESC, e.id ASC
        ) AS rn
    FROM employees e
    JOIN departments d ON e.department_id = d.id
)
SELECT 
    department_name,
    employee_id,
    first_name,
    last_name,
    salary
FROM ranked_employees
WHERE rn = 1
ORDER BY department_name;

Explanation

  1. Common Table Expression (CTE): The ranked_employees CTE joins the employees and departments tables to combine employee details with their department names. The ROW_NUMBER() function assigns a unique rank to each employee within their department. Employees are ordered by:

    • salary DESC to prioritize higher salaries.

    • employee_id ASC to break ties, ensuring consistent results by selecting the employee with the smallest ID first.

  2. Main Query: Filters the results from the CTE to include only the top-ranked employees (rn = 1) from each department.

  3. Ordering: The final result is ordered by department_name to meet the specified output requirement.

Best for you