❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Learning Notes #62 – Serverless – Just like riding a taxi

19 January 2025 at 04:55

What is Serverless Computing?

Serverless computing allows developers to run applications without having to manage the underlying infrastructure. You write code, deploy it, and the cloud provider takes care of the rest from provisioning servers to scaling applications.

Popular serverless platforms include AWS Lambda, Azure Functions, and Google Cloud Functions.

The Taxi Analogy

Imagine traveling to a destination. There are multiple ways to get there,

  1. Owning a Car (Traditional Servers): You own and maintain your car. This means handling maintenance, fuel, insurance, parking, and everything else that comes with it. It’s reliable and gives you control, but it’s also time-consuming and expensive to manage.
  2. Hiring a Taxi (Serverless): With a taxi, you simply book a ride when you need it. You don’t worry about maintaining the car, fueling it, or where it’s parked afterward. You pay only for the distance traveled, and the service scales to your needs whether you’re alone or with friends.

Why Serverless is Like Taking a Taxi ?

  1. No Infrastructure Management – With serverless, you don’t have to manage or worry about servers, just like you don’t need to maintain a taxi.
  2. Pay-As-You-Go – In a taxi, you pay only for the distance traveled. Similarly, in serverless, you’re billed only for the compute time your application consumes.
  3. On-Demand Availability – Need a ride at midnight? A taxi is just a booking away. Serverless functions work the same way available whenever you need them, scaling up or down as required.
  4. Scalability – Whether you’re a solo traveler or part of a group, taxis can adapt by providing a small car or a larger vehicle. Serverless computing scales resources automatically based on traffic, ensuring optimal performance.
  5. Focus on the Destination – When you take a taxi, you focus on reaching your destination without worrying about the vehicle. Serverless lets you concentrate on writing and deploying code rather than worrying about servers.

Key Benefits of Serverless (and Taxi Rides)

  • Cost-Effectiveness – Avoid upfront costs. No need to buy servers (or cars) you might not fully utilize.
  • Flexibility – Serverless platforms support multiple programming languages and integrations.
    Taxis, too, come in various forms: regular cars, SUVs, and even luxury rides for special occasions.
  • Reduced Overhead – Free yourself from maintenance tasks, whether it’s patching servers or checking tire pressure.

When Not to Choose Serverless (or a Taxi)

  1. Predictable, High-Volume Usage – Owning a car might be cheaper if you’re constantly on the road. Similarly, for predictable and sustained workloads, traditional servers or containers might be more cost-effective than serverless.
  2. Special Requirements – Need a specific type of vehicle, like a truck for moving furniture? Owning one might make sense. Similarly, applications with unique infrastructure requirements may not be a perfect fit for serverless.
  3. Latency Sensitivity – Taxis take time to arrive after booking. Likewise, serverless functions may experience cold starts, adding slight delays. For ultra-low-latency applications, other architectures may be preferable.

POTD #15 – Count all triplets with given sum in sorted array | Geeks For Geeks

4 January 2025 at 18:14

Problem Statement

Geeks For Geeks : https://www.geeksforgeeks.org/problems/count-all-triplets-with-given-sum-in-sorted-array/1

Given a sorted arrayΒ arr[] and a target value, the task is to count triplets (i, j, k) of valid indices, such that arr[i] + arr[j] + arr[k] = target and i < j < k.


Input: arr[] = [-3, -1, -1, 0, 1, 2], target = -2
Output: 4
Explanation: Two triplets that add up to -2 are:
arr[0] + arr[3] + arr[4] = (-3) + 0 + (1) = -2
arr[0] + arr[1] + arr[5] = (-3) + (-1) + (2) = -2
arr[0] + arr[2] + arr[5] = (-3) + (-1) + (2) = -2
arr[1] + arr[2] + arr[3] = (-1) + (-1) + (0) = -2


Input: arr[] = [-2, 0, 1, 1, 5], target = 1
Output: 0
Explanation: There is no triplet whose sum is equal to 1. 

My Approach:

Initially i tried to approach the problem, similar to this. All testcases but 1 passed. Initial time complexity is O(n3). Failed 6 times.



class Solution:
    def countTriplets(self, arr, target):
        hash_set = {}
        total = len(arr)
        cnt = 0
        
        # Build the hash_set with indices for each value in arr
        for i in range(total):
            if arr[i] not in hash_set:
                hash_set[arr[i]] = []
            hash_set[arr[i]].append(i)
        
        # Iterate through all pairs (itr, jtr)
        for itr in range(total):
            for jtr in range(itr + 1, total):
                rem = target - arr[itr] - arr[jtr]
                
                # Check for remaining value in hash_set
                if rem in hash_set:
                    # Use binary search to count indices greater than jtr
                    indices = hash_set[rem]
                    low, high = 0, len(indices)
                    
                    while low < high:
                        mid = (low + high) // 2
                        if indices[mid] > jtr:
                            high = mid
                        else:
                            low = mid + 1
                    
                    cnt += len(indices) - low

        return cnt






Then after reading blogs, switched to Two Pointer method



class Solution:
    def countTriplets(self, arr, target):
        n = len(arr)
        res = 0
 
        for i in range(n - 2):
            left = i + 1
            right = n - 1

            while left < right:
                sum = arr[i] + arr[left] + arr[right]
    
                if sum < target:
                    left += 1
    
                elif sum > target:
                    right -= 1
    
                else:
                    ele1 = arr[left]
                    ele2 = arr[right]
                    cnt1 = 0
                    cnt2 = 0
    
                    while left <= right and arr[left] == ele1:
                        left += 1
                        cnt1 += 1
    
                    while left <= right and arr[right] == ele2:
                        right -= 1
                        cnt2 += 1

                    if ele1 == ele2:
                        res += (cnt1 * (cnt1 - 1)) // 2
                    else:
                        res += (cnt1 * cnt2)
    
        return res






SSH – A View

By: Gowtham G
4 March 2024 at 07:32

SSH, also known as Secure Shell or Secure Socket Shell, is aΒ network protocolΒ that gives users, particularly system administrators, a secure way to access a computer over an unsecured network.

By default SSH is installed in linux.We can check the version by using this below command.

ssh -V

Otherwise we can install it by..,

sudo apt install openssh-server openssh-client
sudo systemctl status ssh

Also we can check the status of SSH.The default Port Number assigned by ssh is 22.We can change it in the configuration file also we can allow or deny users or groups and Alive Interval in the configuration file.
NOTE :
Restart the ssh after configured.

nano /etc/ssh/sshd_config
AllowUsers user_name
AllowGroups group_name

To allow permissions to specific users and groups to access ssh.

DenyUsers user_name
DenyGroups group_name

To deny the permission to specific users and groups.

LoginGraceTime specify_time

(i.e) 1m ==> 1 minute.
If a user can’t make a successful login with the specific time,it will not allow the user to enter into the remote machine.

ClientAliveInterval 600
ClientAliveCountMax 0 ==> Default it is zero.

(i.e) 600 ==> 600 seconds = 10 minutes.
The tunnel will break after the mentioned time (600 seconds) if there is no actions were performed by the user in the server.

NOTE: Restart the service once changes are made in configuration file.

ssh user_name@remote_ip
ssh user_name@remote_hostname -p port_number

To connect to the ssh server we must need three credentials of the remote machine.

  • Username.
  • Password.
  • Ip Address or Hostname.
scp file_to_transfer user_name@hostname:/path_to_copy_the_file_in_remote_server

scp -P specific_port_number file_to_transfer user_name@hostname:/path_to_copy_the_file_in_remote_server ===> For using Port Number other than 22.

Example : scp hello.go student@ip_address:/home/student/Checking_scp_command/
scp -P 2027 hello.go student@ip_address:/home/student/Checking_scp_command/

SCP ==> Secure Copy.
To copy a file securely to the remote server’s directory named β€œChecking_scp_command”.

scp user_name@hostname:/path_of_the_file/file /path_to_copy_in_local_system

scp -P specific_port_number user_name@hostname:/path_of_the_file/file /path_to_copy_in_local_system ===> For using Port Number other than 22.

Example : scp student@ip_address:/home/student/Checking_scp_command/Remote_to_Local.txt /home/g/Downloads/
scp -P 2027 student@ip_address:/home/student/Checking_scp_command/Remote_to_Local.txt /home/g/Downloads/

In this command,we are going to get the file from remote server to our local system.

NOTE:

There is a command called β€œssh-keygen” which generates a unique key for accessing the remote server via local system.
This never ask passwords to enter into the system,But be careful of handling this key.If you are not familiar with this method,just practice with the password type few times.
Keygen will be discussed later in our next post.Stay tuned…

That’s it..!

SSH – A View

By: Gowtham G
4 March 2024 at 07:32

SSH, also known as Secure Shell or Secure Socket Shell, is aΒ network protocolΒ that gives users, particularly system administrators, a secure way to access a computer over an unsecured network.

By default SSH is installed in linux.We can check the version by using this below command.

ssh -V

Otherwise we can install it by..,

sudo apt install openssh-server openssh-client
sudo systemctl status ssh

Also we can check the status of SSH.The default Port Number assigned by ssh is 22.We can change it in the configuration file also we can allow or deny users or groups and Alive Interval in the configuration file.
NOTE :
Restart the ssh after configured.

nano /etc/ssh/sshd_config
AllowUsers user_name
AllowGroups group_name

To allow permissions to specific users and groups to access ssh.

DenyUsers user_name
DenyGroups group_name

To deny the permission to specific users and groups.

LoginGraceTime specify_time

(i.e) 1m ==> 1 minute.
If a user can’t make a successful login with the specific time,it will not allow the user to enter into the remote machine.

ClientAliveInterval 600
ClientAliveCountMax 0 ==> Default it is zero.

(i.e) 600 ==> 600 seconds = 10 minutes.
The tunnel will break after the mentioned time (600 seconds) if there is no actions were performed by the user in the server.

NOTE: Restart the service once changes are made in configuration file.

ssh user_name@remote_ip
ssh user_name@remote_hostname -p port_number

To connect to the ssh server we must need three credentials of the remote machine.

  • Username.
  • Password.
  • Ip Address or Hostname.
scp file_to_transfer user_name@hostname:/path_to_copy_the_file_in_remote_server

scp -P specific_port_number file_to_transfer user_name@hostname:/path_to_copy_the_file_in_remote_server ===> For using Port Number other than 22.

Example : scp hello.go student@ip_address:/home/student/Checking_scp_command/
scp -P 2027 hello.go student@ip_address:/home/student/Checking_scp_command/

SCP ==> Secure Copy.
To copy a file securely to the remote server’s directory named β€œChecking_scp_command”.

scp user_name@hostname:/path_of_the_file/file /path_to_copy_in_local_system

scp -P specific_port_number user_name@hostname:/path_of_the_file/file /path_to_copy_in_local_system ===> For using Port Number other than 22.

Example : scp student@ip_address:/home/student/Checking_scp_command/Remote_to_Local.txt /home/g/Downloads/
scp -P 2027 student@ip_address:/home/student/Checking_scp_command/Remote_to_Local.txt /home/g/Downloads/

In this command,we are going to get the file from remote server to our local system.

NOTE:

There is a command called β€œssh-keygen” which generates a unique key for accessing the remote server via local system.
This never ask passwords to enter into the system,But be careful of handling this key.If you are not familiar with this method,just practice with the password type few times.
Keygen will be discussed later in our next post.Stay tuned…

That’s it..!

❌
❌