Friday, July 19, 2024

Interview Question Preperation : Find longest subarray whose sum is equal to K

Software Engineering Practice interview question

Given a array of N elements. Find the length of Longest Subarray whose sum is equal to given K.

 This I believe is one of the most basic Problem Solving(PS) question, which I have found most people get wrong. 

So I wont keep you holding for long, and here is the solution: 



//Returns -1 when no sub-array has sum equal to k. 

int longestSubarrayWithSumK(vector<int> inputArray, long k)     

int l = 0,  max_len = -1, n = inputArray.size();    

    for(int r = 0; r < n; r++){

        long long running_sum= running_sum+ inputArray[r];


        //when the running_sum overshoots the k, try to get it under K and note what the index untill where you are reducing it. 

        while(running_sum> k){

            running_sum -= inputArray[l];

            l++;

        }


     //Once we get the running sum equal to K, check if the current running index length of subarray is more than previously seen subarray length

        if (running_sum== k){

            max_len = max(max_len, r - l + 1);

        }    

    }

        return max_len;

}

Wednesday, July 03, 2024

c # : Tuple Class to store / hold together loosely associated/related items

 Many a  time, you would wonder how to return a pair of objects (not necessarily just 2, but 3,4,5) from a a function and simply creating a new Class to hold together those objects does not make sense / justifiable. 


In such cases, you can make use the Tuple Class in C#, which is  versatile class for the same purpose. 


public (int diameter, double circumference, double area) GetCircleMetaData(int radius) {

    const double PI = 3.1415; var diameter = radius * 2; var circumference = PI * diameter; var area = PI * radius * radius; return (diameter, circumference, area); }


var (d, c, a) = GetCircleMetaData(9);

Saturday, August 05, 2023

Section 80C TMT 2023(PA) – Standard Deduction amounting to a maximum of Rs 8670 under IT rule

With the recently concluded Tax filing season (which if I may draw parallel to the Christmas holiday season enjoyed by one and all), Indians were able to keep their body and soul together in this season. What with this time of the year, where one is expected to scrutinize his own books and ensure that the avg Indian (disclosure I have abstained myself from being tagged in the middle class after I was employed with Microsoft) has done enough for the country by paying their due to nation building, I am sure everyone ruminates over the exercise and feels something like this (for the naïve the red bag is what is carry forward to the next year after the assessment) :



 Well coming back to the topic and I wanted to request (and demand in whatever little capacity that I am safely bestowed upon by the constitution of India), I request the honorable FM, to introduce a new section 80C TMT 2023(PA), which should be formulated as follows :

80C TMT 2023 : Deduction in respect of contributions on payment towards high inflation grocery and daily commodities

(1)In computing the total income of an assessee, being an individual or a Hindu undivided family, there shall be deducted, in accordance with and subject to the provisions of this section, the whole of the amount paid in the previous year, being the aggregate of the sums referred does not exceed Rs 8670, and is made towards purchasing daily grocery and Kirana items where they shoot the inflation rate of 5% on the govt approved MRP/MSP.

  

While this is still in draft and I will ask my fellow citizens add more subsections and clauses to cover other important items, but the intention is clear. 

On a related note, Red seems to be the color of the season(gold and silver is passe) with party organizers flooded with requests to have red themed parties.

 Virgin Bloody Marry the cocktail of the month. 


PS : Yours truly recently purchased Rs200/Kg tomatoes this season thrice already.

PS2: TMT – stands for tomatoes, PA – Parag Agrawal , would be good to have your name immortalized as part of this section.



Thursday, April 13, 2023

[Solved] Failed to load plugin '@typescript-eslint' problem while running a React app

 If you have ever run into this problem while working on a react project, and it seems to be working for others in your team, 

Error message: 


Failed to load plugin '@typescript-eslint' declared in '.eslintrc » eslint-config-react-app#overrides[0]': Cannot find module 'typescript'
Or 
Failed to compile.

[eslint] Failed to load plugin '@typescript-eslint' declared in 'package.json » eslint-config-react-app#overrides[0]': Cannot find module './Base'

then after trying out multiple solution and scouring different forums, I found out that simply running this on the command prompt in the project folder did the trick for me.

D:\ParagDemos\ClientPaymentDemoApp\ClientApp>npm install typescript-eslint

up to date in 7s

247 packages are looking for funding
  run `npm fund` for details
D:\ParagDemos\ClientPaymentDemoApp\ClientApp>

And it did not even change the package.json for me. 




Sunday, January 23, 2022

Prisha and the Tooth fairy - Part 2 aka A practical guide for negotiating the deal between the child and the fairy)

 Well, some of you reached out to me to elicit more information on what was offered by the Tooth Fairy (TF) for the exchange of the precious milk tooth, after the ill-fated event which soon could have coalesced into a cataclysmic fiasco. (yeah we did find out the tooth and the tooth fairy was saved from dusting the room for scouting the tooth). 

For the others (uninformed ladies and gentleman) , I will soon share [Placeholder - Prisha and the lost Milk tooth]

I can deduce the motive for such inquiries. They(inquirers) broadly can be pigeonholed into two groups: 

  • Just plain inquisitive : They just want to know what do tooth fairies offer. What is that Prisha got? (eg, Grand parents/family/Prisha's friends)
  • New Parents in town/Experienced ones but want to know what all can tooth fairy offer: any additional setup you need to offer for this farce. 



Well to the folks who belong to first category, well the TF offered her a envelope loaded with cash. Well it knew it was visiting the Agarwal household, and what can be more pragmatic, convenient and handy than cash $$.







 One is saved from all the fretting and wailing if the TF doesn't get what the child really wanted. So you offer him the cash and take them out to a shop and buy. Or you can be the ingenious kind and order the needed thing from your fav store /hand over to the TF. 

For the folks belonging to the second category: the answer is: it can be anything which is reasonable and should lie somewhere in-between the He/She has been asking for it often vs the birthday present. Coz lets be real, most likely the child has more teeth's in line ready to fall off. You don't want to raise the expectations, and then they would be like,"What? Last time she/TF gave me X(really expensive gift you bought) , and now this. This must have been a mix up. I want my tooth back. Call her". And you don't want to be scouting for TF's customer care number for a exchange. 



 But to be really honest, children dont really like cash. They like gifts in kind. And Prisha did complain a bit and I tried to rationale with her, what if she(TF) bought you a Pictionary set or Uno cards, which you already have and then it would be useless/redundant. She saw the point, and somehow I was able to convenience her that a tooth brush could also have been on offer. So next time, she needs to keep a letter ready along with the tooth, so that the TF can order get it for you. 

PS: This also got me ruminate on the gifts TF bought me for my precious milk tooth's and why was it that my last two teeth's weren't bought back. May be I will write about it another time. 

PS2: Happy New year everyone. 


Src : https://imgur.com/gallery/fmyvlLm

Wednesday, December 01, 2021

How to get Lenovo Make and Model for Warranty validation on Lenovo Website

 Sometimes you may find yourself in a position to check the warranty remaining on your Lenvovo Asset/Laptop/Machine. 

You can run 

C:\TestFolder\Parag> wmic bios 

on a windows command prompt to get that information, which can be looked up on Lenovo website for validation remaining. 

this is how the information looks like 


You should copy the serial number and look it up at Lenovo  warranty look up site. 

Another way to look up this information is via GUI. Read more about it here

Tuesday, September 29, 2020

How to Do APP to APP based authentication for Azure AAD applications

Sometimes you need to do app to app based authentication in your applications/services to prevent unauthorized access to your services . Example you may have a service A which needs to be accessed by another service, you can either configure a username/password based authentication or certification based authentication or a simple app to app based authorization. 

To enable Azure AAD App to App based authentication, you need to do the following changes : 

1)  First in your issuer / authorizing apps app manifest, declare some roles which can be applied to applications. 

{
            "allowedMemberTypes": [
                "User",
                "Application"
            ],
            "description""Some app role which also needs app based auth",
            "displayName""SERVICE_APPLICATION_ROLE",
            "id""23d29786-d213-40f6-ad11-ddf9f7d71d30",
            "isEnabled"true,
            "lang"null,
            "origin""Application",
            "value""SERVICE_APPLICATION_ROLE"
        },

2) Next in your consumer /contacting service request access to the producer/servicing access from MS portal API permissions section

Pic 1



3) in your consumer/client service have a configuration section defined as :

 "AzureAdConfig": {

    "ClientId": "5186aaaf-d222-458d-83c1-faa73773bbc",

    "ClientSecret": "SOME-clIenT=SEcret.7&---",

    "TenantId": "a263c89-4222-4555a-222c-6fe11111cb"

  },

 Next bind this section in your application config as : 

AzureAdConfig azureAdConfig = new AzureAdConfig();
this.configuration.Bind("AzureAdConfig", azureAdConfig);

and then authenticate it as follows : 

var app = ConfidentialClientApplicationBuilder.Create(config.ClientId)

               .WithClientSecret(config.ClientSecret)

               .WithAuthority(AadAuthorityAudience.AzureAdMyOrg)

               .WithTenantId(config.TenantId)

               .Build();

//auth scopes API is the api guid of the authorizing app (check image below Pic 2)

List<string> authScopes = new List<string> { "api://f1ffff77-f224-456a-8222-3777773/.default"};

var result = await app.AcquireTokenForClient(authScopes).ExecuteAsync();

            return result.AccessToken;

Next you can use this token to make all service to service calls 


Pic 2


Interview Question Preperation : Find longest subarray whose sum is equal to K

Software Engineering Practice interview question Given a array of N elements. Find the length of Longest Subarray whose sum is equal to give...