LongCut logo

Last Minute Python Interview Prep Kit (Data Analysts and Data Engineers)

By Ankit Bansal

Summary

Topics Covered

  • Focus on Fundamentals, Skip Advanced Topics

Full Transcript

Hello everyone, welcome back to the channel. Hope all of you are doing good.

channel. Hope all of you are doing good.

So this is the fourth video in our last minute interview prep series. First one

was SQL, then data modeling and data warehousing and third one was a spark and thank you so much for so good response on those videos. Now if you see those three videos, you will see a lot

of comments are asking about Python video. So today we are going to do for

video. So today we are going to do for Python last minute interview prep video.

Now what we will do in this first I will explain you I will tell you that what all topics are important right kind of I will give you a road map that these topics are important what all you should

learn as a data analyst what are the topics and as a data engineers what are the topics and then I will start solving some questions one by one I will start

with some easy questions then medium then difficult we will talk about how to optimize these questions how to optimize these solutions solutions and how to check what is the time complexity.

Right? So when you go for interviews, you might solve a question but you might have not got the uh as per expectation because it might be taking more time than expected. Right? So we will discuss

than expected. Right? So we will discuss how to approach those kind of questions.

Right? Make sure if you are not very confident about Python, you practice along with me. I will show you some resources also along the way where you can practice Python questions for free.

Right? So focus on this video, watch end to end and by the end of the video you will feel very confident going into the interviews. Okay. So let's get started.

interviews. Okay. So let's get started.

First let's see what are the important topics and what are the things that you should know before you start solving the questions for interview. Okay. So I have divided Python interviews in two part.

One is coding and one is analytics. So

we will see what what is there inside each one of them. So if you go to coding right there are two things one is what are the topics you should learn right and then what are the patterns basic

patterns that can be asked in your interviews okay so if I go to topics these are the bare minimum topics that you should learn and these are the good

enough topics to crack any data analyst or data engineer interview at least 95% of the interviews revolved around these topics right so we have variables if

else loop loops, strings, list or arrays dictionary hashmaps right?

Sets, tpples, functions, working with file, exception handling and bigo notation, right? So these are the things

notation, right? So these are the things if you know, if you are very much clear about these things, then you can easily solve questions um asked in data analyst

and data engineer interview, right? They

are advanced topics. There are advanced data structures like for example you have cues stacks you have more advanced topics like dynamic programming link list you can learn that but I'm telling

you 90 95% interviews can be solved can be cracked using these topics right because see most of the interviews for data engineers and data analyst are not

very uh hard python questions right they are basic questions or medium questions they don't ask hard questions very specific data engine roles right

maybe some platform engineers or maybe 5 to 10% maximum right those required those advanced concepts for you as a data analyst or data engineer 90% of the

time these topics are enough right so focus on these topics if you are very good on all of these and the patterns that we will discuss then you can learn more things but if you learn these

things then it will be more than enough right so you should learn this this is Python fundamentals how to work with list, what is dictionary, what is set, tapulse, what is function, how to work with

files. So all of these things you should

files. So all of these things you should be very comfortable. I will not teach you in this video. This is a interview prep video. So if you don't know all of

prep video. So if you don't know all of these things then you cannot do interview prep, right? So you have to understand these things first. Right? If

you are a complete beginner in Python, right? you don't know all these topics,

right? you don't know all these topics, you are not very good with fundamentals, you don't feel very confident, then you can simply go to namastasql.com go to courses and then we have a Python for

data engineering course. In this we have covered all of the important things that you need to know as a data analyst or data engineer for Python right we have covered what all topics I just discussed

with you everything is there and apart from that also we have lot of other things right so for example all the basics are there working with numpy pandas and we have in-depth pandas video

as well we have how to we have uh sessions on how to create ETL pipelines work with databases different kind of files how to work with AWS all of those

things are also there. These things are mostly data engineering focused right.

They will not ask these things in interview but in your day-to-day work you will have to do all of these things right interview will focus on from here from second lecture till I think uh

pandas right maximum till here right all of these things are your day-to-day work that you have to apply right so it is a end to end project which will really

help you in getting your python hands on and feeling confident right you can see the reviews as well we have very good reviews for this course and All are

organic reviews. I don't ask anyone to

organic reviews. I don't ask anyone to uh give some good reviews or fake reviews, right? So you can check it out

reviews, right? So you can check it out and Okay. Okay. So once you are

and Okay. Okay. So once you are comfortable with these things in Python, then there are different patterns on which questions can be there in your

interview. Right. So this is very common

interview. Right. So this is very common pattern like you have a dictionary and you have to count something, right? We

will see lot of examples on that. Then

there are problems on array plus dictionary. Right? When I say array in

dictionary. Right? When I say array in Python, arrays are list basically, right? So, array and list is the same

right? So, array and list is the same thing. So, if I am saying array or list,

thing. So, if I am saying array or list, they are both same thing. Similarly,

dictionary and hashmap, it is the same thing, right? In Python, we call it

thing, right? In Python, we call it dictionaries. In some other languages

dictionaries. In some other languages like Java, they are hashmap, but they are the same thing.

Okay. Then we have string problems, right? So, there are lot of questions

right? So, there are lot of questions asked about a string, how to work with a string. And there will be a good

string. And there will be a good question where you have to manipulate this thing and do lot of things right then you should know what is binary search right what is two pointers so

some problems can be easily solved using two pointers without two pointers the solution will not be optimized but with two pointers approach we can have a

better uh efficient solution then we have set based problems right sliding window sorting reversal and then passing logs this is specifically for uh data

engineers right how to pass logs, how to get some information from the logs. So

these are the important topics right that we need to solve problems on right.

So I will start with the basic things and then I will move forward with more things. Now I'm not going to cover these

things. Now I'm not going to cover these things right. I will talk about time

things right. I will talk about time complexity right because when I'm solving questions I have to tell you okay this is the time complexity and if you solve this way this is the time complexity. So when I'm solving

complexity. So when I'm solving questions that time I will tell you what is time complexity and how to go about it how to think about time complexity.

Okay. So these are the things these are most important and cover 90% of the interviews. Right. So you should be good

interviews. Right. So you should be good after after finishing this video. Okay.

Now second thing is analytics. Now in

analyt analytics basically for data analyst some questions can be asked on pandas right and maybe data engineer.

But this portion is typically rare right most of the companies product based companies service based companies will focus on these things because as a data engineer you will not be working on

pandas mostly you'll be working on maybe pi spark right but you should know it I mean working with pandas you should know it lot of organization still uses it but I don't think many of the interviews ask

this they can ask but I'm telling you mostly they are not asked mostly it is around these two topics right these are the topics and these These are the patterns. Now in pandas also we have few

patterns. Now in pandas also we have few things that you should focus on if you are learning pandas right group by aggregation filtering sorting and top

end merging or joins how to write ETL using pandas window rank functions right in this video I will not cover this. If

you think uh there's a good amount of uh interviews which are asking these things I can create a separate video right but I want to focus on the most important 90% of the things right. So we will

focus on these things today right and maybe in the next video I can talk about this if there's a good amount of requirement in the comment section. Okay

I hope this is clear. Uh let's start with the uh practice sessions.

Okay. So let's understand what is time complexity. Right. Let's read the

complexity. Right. Let's read the definition and then I will break it down step by step. So time complexity is a way to measure how the number of

operations in an algorithm or program you can write anything right grows as the input size increases.

It does not represent actual time taken but how efficiently an algorithm scales.

It helps compare algorithm independent of machine speed or programming language. Now lot of people think that

language. Now lot of people think that time complexity means the time taken by a program which is wrong because time taken by a program is dependent on

machine right if you are running a python program or any program on let's say 8 GB machine it will take some amount of time but if the same program

you run on let's say 16 GB or 32GB machine then it will take less time right so m the time taken By a program

is not fixed. It it is dependent on machine. But time complexity we measure

machine. But time complexity we measure independent of machine. Right? It does

not represent actual time taken. But how

efficiently an algorithm scales, right?

So this thing should be clear, right?

That time complexity is independent of a machine speed or which programming language we talk about, right? we just a relative measure to check how good my

program is. So we will understand with a

program is. So we will understand with a simple example first right. So let me move on to uh Visual Studio and I I'll explain in a very intuitive way. Let's say you have

this array right array or list in in generally most of the programming languages you say array. In Python it is a list right? List is a group of elements together right? So we have 1 2

3 4 5 6. Okay. So this is my input right? This is my input. This arrays is

right? This is my input. This arrays is my input. Let's say I want to find sum

my input. Let's say I want to find sum of all the elements of this array.

Right? So what I have to do I have to start with this then go to this add this then add this then add this then add this then add this. So if there are six

element I will have to do six operations right. So let let's write a program. So

right. So let let's write a program. So

let's say my sum is or let's define a function define get sum right and it will take one array as

input right so what we will say we will initialize my total sum so I will say total sum equal to zero right now I will

run a loop over it right because I want to go to each element so what I want to do I want to run a loop over this list Right? I want to go

over iterate over this loop and go to each element and sum all the numbers. So

I will say for num in right air

and then I will say total sum right equal to whatever was sum was there earlier plus this num whatever num I get

right. So I'm getting 1 2 3 4 5 6 right

right. So I'm getting 1 2 3 4 5 6 right and I will just run over it and then I will say return my total sum.

I hope you understand this. So I want to see how many times I'm doing this operation right. So I'll say print and I

operation right. So I'll say print and I will just call this function now. So

I'll say get sum and I will pass array this array whatever we have declared here right and let's see

okay so it is giving me 21 so if you sum all of this it will give you 21 now how many times we are running the loop or how many times we are doing this operation right so let me print it so I

will say iteration we will see how many iteration are there iteration equal to zero right and then in the loop I will say iteration

equal to iteration + 1 and then I will say print iteration and then iteration right I'm just showing you how many times we are

running this loop so if you see iteration 1 2 3 4 5 6 right so there are six elements I'm running this loop six times so I'm doing six operations right

let's say if I increase one more element here right. So how many times this will

here right. So how many times this will run? Seven times. Which means as the

run? Seven times. Which means as the size of my input is increasing the number of operations are increasing linearly. Linearly means if

increasing linearly. Linearly means if there are eight elements then number of operations are eight. I'm doing eight operations. If it is nine then nine

operations. If it is nine then nine operations. If it is 1 million, let's

operations. If it is 1 million, let's say in this array I have 1 million elements, then I will run this loop on 1 million elements and there will be 1

million operations, right? So this is known as a linear time complexity. We

represent time complexity by big notation O and then N.

Right? So this is N means linear time complexity. As you increase the size of

complexity. As you increase the size of the input in the same proportion your number of operations will increase right now it is irrespective of machine right

the way we are calculating this complexity bigo n is irrespective of machine right we are saying as the number of input size is increasing is the number of operation increasing now

it will be true for 8 GB system or 16 GB system or for Java or Python or any language right so this This is this is why this is platform or program and

speed independent right this is how we calculate the time complexity great so if I go back here it is saying

is linear time right so example is loop over the list so you can draw a chart right so if you see this chart so on the

I-axis I'm saying input data size right as the size of the input data increasing how the number of operations are increasing ing. So in case of linear

increasing ing. So in case of linear time complexity right O this is linear.

So as the input size increasing in the same manner my number of operations are increasing. So if you see this is x

increasing. So if you see this is x equal to y right the number of input data size equal to number of operations.

So this is the x equal to y line on your chart. Right? So this is your o n right?

chart. Right? So this is your o n right?

Lesser the time complexity better your program is. Right? Now let's take

program is. Right? Now let's take another example. So this we understand.

another example. So this we understand.

Now let's say my requirement is I want to get the first element of my array. So

this is my array, right? Let me just comment this.

So this is my array and I want just the first element of the list. So what I will do? L and first element will be

will do? L and first element will be zero. Right? And I will print this. So

zero. Right? And I will print this. So

let's print it.

Okay. So if you see I'm getting one. So

this is giving me the first element. Now

let's I increase the number of elements in my array.

Right? Now if I again run this, I'm always doing one operations. It

doesn't matter how many elements are there in my list. Right? The number of operations will be just one. get the

first element of the list. So this is known as constant time complexity that the time taken or [clears throat] the number of operations done by your

program is fixed irrespective of how small or big your input is. Right? So

this is known as 01. It will take fixed 01 time. Right? So this is your constant

01 time. Right? So this is your constant time complexity where it is constant in respective your input size and if you look at the chart this is my 01 right

this is flat doesn't matter that how big is your input data size if we keep increasing the number of operations is constant which is one so there will be only one operation so this is known as

constant time accessing an index in a list right so this is your O1 O so is better than O1 right because it will

just one operation right so always if you go up it is the worst time complexity down it is the best so O1 is best and

after that O we will talk about other time complexities as well right now let's take another example of O N right let's say I have this array

now right now what I want to do right this this can be a sorted or unsorted array right let not take it's sorted. So

let's take it 15, 11, 12 something.

Okay. Now what I want to do, I want to find index of a particular number.

Right? Let's say uh someone is telling tell me where is 15. Right? On what

index 15 is there? Right? So my target is this that I want to find index of 15 in my list. So how I will do it? Let's

write a program. So I will say def find index right it will take two input one is array and another is target right

what what you want to search right this is also known as linear search so I'll just explain about it so what I'm saying that okay loop over it and check

wherever the target is so I'll say for let's take i in range right these things

you should know already length of length of array right so I'm saying if

l i right is equal to target value right whatever target I have then return

that index value right that's it so this is my linear search you can call it linear search I'll tell you what is linear search linear search

okay now we will see what is the time complexity of this okay so now I will just call this I'll say linear search

and let's call I'm passing array right and my target right great let's print it.

So target is 13. Target element, let's call it target element.

And I'll just pass this here.

Okay, let's see what it will give me.

Okay, so it is giving me nine, right?

Nine means. So if you count here 0 1 2 0

1 2 0 1 2 3 4 5 6 7 8 9 right. So it is at ninth index. Right? So the 15 is at

ninth index. Now let's say I search for

ninth index. Now let's say I search for seven.

So it will give me where is seven? Seven

is at sixth index. Great. Now let's say how many iterations we are doing here.

So again we are running we are comparing this. So this is one operation right we

this. So this is one operation right we are running this inside for loop. So I

will say iteration equal to zero right and then iteration equal to iteration + 1.

and I'll just print iteration.

Okay, let's run this.

So if you see this is running six iterations, right? Uh

iterations, right? Uh D.

Okay, it has to be outside the loop. My

bad.

Okay, so initialization is outside the loop and then uh okay, it has to be outside actually if condition because only it will print

only when we satisfy the condition, right? So iteration equal to iteration

right? So iteration equal to iteration plus one. Great.

plus one. Great.

Uh okay, print is again here. So let me just fix this.

Okay, so if you see 1 2 3 4 5 6 7. So it

is running seven times right. So now if you see the number of operations is dependent on where my element is. If I

just let's say search for two right so it will run only two times iteration one iteration two. So there are only two

iteration two. So there are only two operations right now how I will tell what is the time complexity right? Time

complexity depends on that which element you are searching. If you are searching for two here in this list then it is at second element only two operations right. If I search let's say 11 then it

right. If I search let's say 11 then it will learn maybe eight or nine times right. So when we talk about big

right. So when we talk about big notation big notation we consider the worst time complexity. Worst means if someone is searching 12 that is the

worst case right when someone is searching 12 that time we have to run over the full list. Right? So if I run this it is running 12 times right. So

when I talk about a big notation it means we are considering the worst time complexity that at the worst how many operations I have to do. So again this

is a on because if the number of elements are increasing if it is 50 then my worst will be one ele one more operation right so it is it is running

10 times if I increase one more right let's say 20 and again so it is again 10 times right so it is running 10 times if

I make it 20 this is my worst time complexity which will be 14 right we have to go till here right So whenever you you think that what is the time

complexity of this program you have to take the worst case. In best case it will be 01 right because maybe you every time you find the first element right someone is searching one right you will

get in the first iteration itself but you have to consider the worst time complexity and in this case it will be O N right so this comes into the category

of linear search that we are searching a element linearly right linear means first element then we check second element then third then fourth then fifth then sixth and so on right so this

is your uh linear search. Great. Now let's move on to the next time complexity right which is let's talk about O N² right

then we will move on to the login. So

what is O² quadratic time generally when you have to run the nested loops right loop over loop that time this quadratic comes into picture and it is very bad

kind of time complexity right because as the input size increases you all number of operations will increase square of times right so for example if you are

increasing from 10 to 11 then maybe your operations are increasing from let's say 10 to 100 right I'm just giving one example right so let's understand that

so I I have written a simple program to find a duplicate transactions right so let's say you have a this transactions you have a right and then what you have

to do you have to find the duplicate transactions so what I have to do for example 101 1 these are duplicate so I have to get the index of these so let me

run this and show you okay so I'm getting answer is 0 to 1 14. So it is telling at index zero and index two they

are duplicates. Similarly at index one

are duplicates. Similarly at index one and index 4 they are duplicates. Right?

So this is what it is returning list of tpples. Great. So let's think about it. How do we solve this problem?

about it. How do we solve this problem?

So what I have to do I have to pick first element with a very brute force method. Right? First element and compare

method. Right? First element and compare with all the elements. Right? So 101 203 101 44 203. So this element I will pick and compare with all the elements. Then

in the second iteration I will pick this element and compare with all other elements. In the third iteration 101 and

elements. In the third iteration 101 and then I will compare with other elements.

Right? So this is what we have to do. So

this is a loop under loop. Right? So

this is a nested loop. So I'm running a loop on this list first. So if you see on transaction and then again I'm running on the another loop on transactions and comparing right I'm not

going to explain this program but basically we have to run loop two times right whenever you see two times loop

mostly it is a n² time complexity right let's see how many iterations we have to done so I'll say iterations

equal to zero right and we will say iteration equal to iteration + one. Now

I will so all the operations we are doing inside the second lasted loop right so I have to print here right so for every element in outer loop inner loop will run right so all the

operations are happening within the inner loop let's say iterations + 1 and let's print it print

iteration iteration great so if you see I'm doing 10 iterations right I'm doing 10 iterations for this program. Now let's increase the size of

program. Now let's increase the size of my transaction. Let's say I increase one

my transaction. Let's say I increase one more let's say 100. Now what will happen? Each in each loop I have to

happen? Each in each loop I have to iterate one more time. So when 101 was comparing it was comparing with four elements. Now it has to compare with

elements. Now it has to compare with five elements. Again when 203 I was

five elements. Again when 203 I was comparing earlier I was comparing with three elements. Now I have to compare

three elements. Now I have to compare with four elements. Right? So as the number of input is increases it will run loop more number of times right. So

let's see. So there are 10 iterations and I increased one more element.

So if you see now we have 15 iterations right. It is not linear that if I

right. It is not linear that if I increase one element then only one one operation will increase. No let me increase one more. Let's say 101 101 is

already there. That's fine. 110

already there. That's fine. 110

anything. So from 15 we will see it is going to 21 right. So this kind of cases where you have loop under loop or you

have a matrix right let's say you have a matrix let's say 1 2 so this is the first element of your matrix second third then you have let's

say 4 5 6 right and then 7 8 9 right if you if you have a matrix n by n right and you have to loop over it right so

three rows in three rows and three columns so there will be nine elements right so it will be O N² you will have to run your loop O N² if you want to traverse over this matrix then you have

to run N into N times right so this is where your O² comes into picture and this is worse than your linear time complexity right because in this as the

input size is increasing this is increasing quadratic time right if you increase three then maybe it is 9 if it is four then it becomes 16 right so Your

best is so far O1 because it is constant. Then you have O N and then O

constant. Then you have O N and then O N². The whole purpose of our Python

N². The whole purpose of our Python programs will be that if we can solve a question with less time complexity. If

we can solve with this with O1 then great. If not then O N. If not then O

great. If not then O N. If not then O N². Most of the Python problems you want

N². Most of the Python problems you want to solve with O N or O login. Right? So

we will see what is O login and then we will come back to it and we'll discuss the final thing. Okay. So what is O log

N right? O login is a time complexity

N right? O login is a time complexity where the number of input size when you increase then the number of operations

also increase but very very slow right it is not like n that if you have let's say 10 then operations will be 10 if you

have 10 size of your input data then 10 operations no maybe if you have 10 it might be let's say two operations when it is 100 then might be six operations right so it is log logarithmic which

means as the input data size is increasing this is also increasing but very slow right so let's see how we can visualize this okay so there's a concept

of binary search right in SQL also if you have learned index you will know how binary search works so how binary search works let's say you have a array I'm I'm

just explaining how this works right let's say you have a array and it is sorted sorted means we know that it will be in ascending order or descending order. So

in our case if you see this is in ascending order 1 3 5 7 8 10 13. So this

is the requirement right that my array is sorted. Let's say my array is sorted.

is sorted. Let's say my array is sorted.

However, however we sorted it, it doesn't matter. But we are getting a

doesn't matter. But we are getting a array which is sorted.

Now I want to find an element in this.

Right? One is we do a linear search.

Whatever we did here, we go to the each element and search it. First, second,

third and wherever we find it, we are done. But because my data is sorted, I

done. But because my data is sorted, I can do a binary search. Right? Now, what

is binary search? Binary search means let's say someone is telling me so we have seven elements in this right someone is telling me that find where is

let's say three or let's say 10 so I want the index of 10 right so one way is linear search we all know but let's say let's see how we can do with binary

search so what I will do I will go to the mid of this array so mid of this array is seven Right? Mid of this array

is seven and I will compare that if this element is equal to seven or not. Right? If it

is not then I will see if it is greater or less than my mid element. Now 10 is greater than [clears throat] my mid element.

My mid element is seven and 10 is greater than my mid element which means 10 will be somewhere in the right.

Right? because this is a sorted so I'm checking with the mid element my target element I want to see where is this 10 so I will check with the mid

element seven right and 10 is greater than 7 which means that 10 will be somewhere in the right so I can skip all these left element I don't have to

search so I will search in 8 10 13 again I will take mid of it and I will check okay if 10 is equal to mid yeah mid is 10 so I will return that value right

let's take another example let's say one I want to search one so I will check with the mid element right if it is equal to mid element I will return that

if it is no now if it is less or more if it is less then I will ignore these things I'll just look into this great

now again I will take mid if it is equal no if it is less then I will come here and then I will again compare then I I will return this right

so basically I'm doing a binary search right I'm not iterating over all the elements I will just divide it check with the mid element then every time my

input size is becoming half and half and half right so let's take this example right so I've written this program for binary search I'm not going to explain

today uh you can learn about it but basically let's see how many iterations we are doing right so I saying iteration zero and iteration equal to iteration + one same thing right and let's see how many iterations

we will do so I'm just running it so if you see it is doing the three iterations right I'm trying to find one my target element is one so it is returning zero

right and three iterations it is doing so I have seven elements iterations are three great now let's add some more

elements it should be sorted so I'll say let's say 20 21 30 33 40 50 70 I just

increased so many elements, right? So we

had till 10. I think seven were there.

Now we have 1 2 3 4 5 6 7. Okay, I

increased 1 2 3 4 5 6 7 8. I added eight more elements. So we have 15 elements

more elements. So we have 15 elements now. And we will see how many iterations

now. And we will see how many iterations are happening to each to see where is one. Right, I'll just run it. You see

one. Right, I'll just run it. You see

only four iterations are happening. So

input size increased almost double from 7 to 8 but iteration increased by only one. Let's search for something else.

one. Let's search for something else.

Let's say I'm searching for 70. Right?

So if I search for 70, there are only three iterations. Right? On the third

three iterations. Right? On the third iteration, it is able to find out.

Right? If I increase more, let's say 100 one 100 100, right? Again, it is just two iteration.

right? Again, it is just two iteration.

It depends on where is your this element but it will be increasing by very less number of operations are increasing because we are dividing into half right

if there are 100 elements we are dividing into 50 first then 25 then 12 then six and then three and then one right so we are not doing 100 operations

just we are doing a binary search right so you can think about something like this this is your binary search right we look at the middle element. If my target element is less than middle element, I

will go to the left. If my target element is greater than my middle element, I will go to the right. Right?

And then again, I will keep doing it until I find my final element, final target. Right? So this is your

target. Right? So this is your login. So capital O log N or logarithmic

login. So capital O log N or logarithmic time complexity. Right? So this is your

time complexity. Right? So this is your login. If you see as the input data size

login. If you see as the input data size is increasing it is increasing but very slowly right as you have very big size 1 million it is almost getting flat right

so this is your logarithmic time complexity example is binary search right so there are other time complexities as well like 2n n factorial

and all but these are the main ones that we need to understand and in fact O² also we will be avoiding it most of the times right most of the times our solution solution should be in O right I

hope this makes sense now last thing I want to tell or two more things I want to tell is one is when I sort something

right so let's say uh uh let's go here or it is fine let's go here let's say this is my array right

or this is my array transactions let me comment it and I want to sort this array Okay.

Right. So I will say transactions dot sort.

Right. And then I will say print transactions.

Okay. And I will print again here.

Okay. So let's see what will happen.

So if you see this is here and now this is sorted. So 100 1 10 1 10 1 10 1 10 1

is sorted. So 100 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 10 1 1 0 1 1 2 0 3 and all. Now this is inbuilt method of sort right but we need

to understand that how much time it will take right what is the time complexity of the sorting so when I'm doing a sorting python is doing some operations internally yeah right there are

different kind of sorting algorithm right but when we are doing this sort this takes bigo n login time complexity n login

so this will be your n login time complexity density which will be better than N² right but worse than O N so it

will be somewhere here between O N log N right it will take more than N operations but better than O N square so your O N log N will be somewhere here

right so your this is how it is so for example if I want to find let's say largest element or smallest element in this transaction

Right. So what I can do I can sort it

Right. So what I can do I can sort it and then if I want the smallest I will take the first element. Right? So I will say sort and then transaction zero. So

my requirement is I want the smallest element in my array. What I can do? I

can do transaction dots sort and then once it is sorted then I will take the first element and that will be my smallest element. Right? So if I just

smallest element. Right? So if I just run this so this is my smallest element.

Right? But if you look at the time complexity, so this is 01, right? So to

get the first element, it is 01.

O1, right? But to sort it, it is O log N login, right? So when we have two time

login, right? So when we have two time complexities like this that this is taking uh so we have two operations one is taking O N login and other is taking O1.

So we take the worst one right. So the

total time complicity we will take it as O N login for this program right sorting plus getting the first element the overall will be considered as O N log N right. So if you have two operations one

right. So if you have two operations one with one time complexity let's say it was O N and another N login. So overall

we will say N login right? So whatever

is the worst that one will be considered right. So this problem we solved with O

right. So this problem we solved with O N log N. But can we make it better? Can

we make this program so that it takes only one iteration and only can we solve it O? Right? So we will solve this

it O? Right? So we will solve this question next time in the next video.

But basically the idea is you should know that sorting is costlier than O N.

You should know that when you are sorting something it is costly than O N.

Right? So your O N log N will be somewhere here.

Okay, great. So, hope you understand it.

So, generally people will just sort the data, right? And they will say, "Okay, I

data, right? And they will say, "Okay, I solved it." But if you can solve it

solved it." But if you can solve it without sorting with O time complexity, that will be best. Great. Now, last

thing I want to discuss is dictionary, right? So when I do a lookup in a

right? So when I do a lookup in a dictionary, so let's say I have my dictionary uh sorry let's say I'm saying employee ID and

name anit uh so this is my key value pair right another key pair value pair is let's say

200 and value is um ath right now this is my dictionary right now when I do a lookup

on a dictionary. Basically, let's say I want what is the name of ID 100. So, I

will say print my dictionary, right? And I will pass uh 100,

right? And I will pass uh 100, right?

Right. So, let's run this. So, if you say it is giving me uncit, right? It is

giving me uncit. Let me comment this everything.

Okay.

So let's run this again. So if you see I'm getting ankit. So what it is doing we are doing a lookup. I'm saying give me the value of key 100. Right? This is

my key value key value. Now you must be thinking that internally this is running a loop right because it will go to first element and see if it is 100 or not.

Then it will go to second element then see if it is 100 or not. Then it will go to the third element. Let's say if there are 1 million. You must be thinking it may be doing a linear search. But that's

not the case, right? When you do a lookup on a dictionary, it is a 01 time complexity. 01 constant.

complexity. 01 constant.

How? So what happens when you create a dictionary, it in memory, right? It it has the all the values. Which means Python will know

the values. Which means Python will know that at what address in your memory 100 is at what address 200 is. So when you say my dictionary 100 it will directly

go to the that address and give you the value. When you say 200 it already know

value. When you say 200 it already know it has all the keys. So it knows that 200 is where in your memory right? So it

will just go there and give you a right.

So this is a constant time complexity when you are when you do a lookup on a dictionary. Right? So this thing you

dictionary. Right? So this thing you have to remember right? So any lookup on a dictionary is 01 constant time complexity.

Great. Now that you understand time complexity, let's solve few more questions. Let's say I have a array or

questions. Let's say I have a array or list and we have some elements and that are unsorted. This is not in sorted

are unsorted. This is not in sorted order. Right? Now my requirement is I

order. Right? Now my requirement is I want to find the largest element or the smallest element anything. So we will solve for the largest element.

the thing will be same for smallest element as well.

Now, how do I find the largest element?

Let me make Okay, so 40 is largest in this case. Okay. So, how do I find it?

this case. Okay. So, how do I find it?

Right. So, one way is very very uh brute force ways I sort this array. So, I'll

say air dot sort. Right? So, once I do it, your array will be sorted. So, if I say print, right? And you see your sort array is

right? And you see your sort array is sorted here, right? 1 9 12 so on and so forth right now I can just take the last element right once it is sorted the

largest element will be at the end right so I can just say s and give me the last element so I can do negative indexing and that will give me 40 right but if

you see what is the time complexity right so time complexity will be o n login right because sorting takes

sorting takes what n login Right? And

this is not the best solution. Right?

And login. Okay.

Okay. So what else we can do? Right. So

it is O N login to sort it and to get this element will be O1. Right. Because

this is fixed. No matter what is the size of your array. If you get the last element that will be O1. So overall time complexity will be O N log N. Right? Now

the whole point is can we make it better right instead of O N log N can we make it O N right can we make it O N so let's

see can we make it or not okay so what I will do I will start iterating over this list right and I will keep track of the

largest element if a next element is largest I will replace the variable with the largest one okay confusing it's okay I'll explain you. So what I'll say I'll

write a function. So define get largest number right let's say this is the function I'm defining and it will take a array as

input right and I'll say right so I'll say largest element or let's say max num is the first element I'm just assuming

that first element is largest so I will say air zero right okay so currently this This is my first element and I have stored

max num as first element which is 20.

Now I will iterate over it. I will check second element. If it is greater than

second element. If it is greater than this then I will assign max num to this number. Then again this then again this

number. Then again this then again this and I will keep doing it. So what I'll do I'll say for

num in right for num in this array right if

num is greater than maximum right in that case I will say my max num is this num you're getting it okay so let

me tell you what is happening so first element is 20 and in max num currently 20 is there right I'll just write it right so in max num 20 is there now I

will iterate over it so first element is 20 right is 20 greater than 20 no right I can start with the first element also but it is fine right because first

element anyway we are taking in maximum okay so this will not go inside it right so it it will be as it is so my maximum will be 20 after first iteration now

after second iteration 12 right is 12 greater than 20? No. So again it will not go. So we will not do anything.

not go. So we will not do anything.

Again 18. Is 18 greater than my maximum?

Maximum is 20. No. Then it will keep going on like that. Let me take this as 21. Right? Okay. Now in the next

21. Right? Okay. Now in the next iteration it will be 21. Right? So is 21 greater than maximum? Yes. 21 is greater than maximum. At that point my maximum

than maximum. At that point my maximum will become 21.

Right? My maximum will become 21.

And that way in the next iteration 30.

Now 30 is 30 is numb. Right? 30 is

greater than my maximum. Yes. Then it

will become 30. And then at 40 it will become 40 and after that it will never replace it because one will be less than maximum. Right? And 9 will be less than

maximum. Right? And 9 will be less than maximum and 30 will be less than maximum. So my maximum will remain as 40

maximum. So my maximum will remain as 40 at the end.

Right. So I have to come out of this loop and I will say return 40 sorry return max now. Right. So if

you see the time complexity here right here if you see I am just running loop once.

Right. So if there are 1 2 3 4 5 6 7 8 nine elements this loop will run nine times and I am doing nine operations.

This is one operation. So again 01 and this is O. So overall will be O N. So O

N is bigger than O N log. Right? So this

is how you have to think about it. Let

me call this function and show you. So

I'll just say print and I'm passing this array. Right? So

let me pass array.

If you see I'm getting 40, right? So

this is how you have to think about it.

Let me just remove this because it is sorted already.

Yeah, now it should be fine and we are still getting 40, right? Let me clear it. And what I will I want to show it I

it. And what I will I want to show it I will print here as well so that you know after every for loop where we are right.

So I'll just say print uh here right print. So we are within for loop outside if condition right. So

if condition is satisfied or not I always want to print what is my maximum value. Right? So let me see what it will

value. Right? So let me see what it will give.

Okay. So it is saying 20. So initially

maximum is 20. Then in second iteration also 20. Third iteration also 20. Right?

also 20. Third iteration also 20. Right?

Now in the fourth station if you see it became 21. Now in fifth it became 30

became 21. Now in fifth it became 30 right? Then it became 40 and after that

right? Then it became 40 and after that 40 is there always maximum because we are doing comparison only when the num is greater than maximum then we are replacing it otherwise it will remain as

it is right. So this is how you have to think about it. Okay great.

So now let's solve next question where instead of largest element I need to find the second largest element right so this is my array right in this if you

see the second largest is after 40 it is 30 so we should get 30 now again think about it right if I need second largest element the easiest way again I can just

sort it so let me take it here right I will sort it and get the second last element, isn't it? I will sort it

and get the second last element and I will get second largest um value. Right?

Let me just comment this for now. And if

you see I got 30, right? Because it will sort first after sorting the 30 will be here and it will give me but again the

time complexity is n login sorry n login right right the sorting takes n login um uh time complexity. So again I want to

solve it with log n right for a better efficient solution.

Okay. So now think about it here we were maintaining a variable max number and we were checking each number if it is greater than max number then we were

replacing the value of max number and that we checked for all the values. Now

here we have to keep track of two variables which is largest and which is second largest. Right? So let's let's do

second largest. Right? So let's let's do it. So I'll just remove it for now.

it. So I'll just remove it for now.

Right? And let me take this function.

Okay. So I will call it get second largest number.

Sorry. Okay. Now what we will do?

We have to declare two variables now.

One is max and one is let's say second max num. Right? So this will be my

max num. Right? So this will be my largest element and this will be my second largest element. Right? For now

right for now I'm just keeping it as zero one and zero. I'll not hardcode it but just to explain what I'm trying to do. For now I'm keeping it as one and

do. For now I'm keeping it as one and this is zero and this is maybe I'm I don't want to keep it one uh let's say three. I'll tell you why I'm doing. So

three. I'll tell you why I'm doing. So

for now consider this is my array right?

And these are two numbers I just declared max equal to 1 and second max is zero for now. Right? We we will not hardcode it because in my array I can have 0 1 or negative numbers as well.

But for this but for this example let's say I know I my smallest value is three here. So I'm just keeping it as one and

here. So I'm just keeping it as one and zero. Right? I'll change it later. Now

zero. Right? I'll change it later. Now

what I will do now I will check each number with maximum. Right? If number is greater than maxum right? If my number so first number is 20 is it greater than

maximum? Yes. So now my maximum will

maximum? Yes. So now my maximum will become num at the same time my second largest number will also change. Right?

Initially my largest is one second largest is zero. Now my largest number will become 20. Right? And what will be my second largest now? Second largest

will be whatever value of the maximum. Right? So

my second largest will be one now right?

So so you have to remember it if I'm getting a number which is greater than my maximum number. So my largest number will become 20 and second largest will

be whatever was max earlier which is one right. So before assigning this value

right. So before assigning this value right so this will become 20. So I I I don't have information about what was my maximum right? If I assign it, my

maximum right? If I assign it, my maximum will become 20. Right? Now at

this point I don't know what was my earlier maximum. So before assigning it

earlier maximum. So before assigning it I will say my second maximum equal to maximum. Right?

My second maximum equal to maximum and maximum 20. Right? At this point the

maximum 20. Right? At this point the maximum will become 20 and second maximum will be one.

Right? So this this is how you have to keep track of it. Okay, great. So, this

will solve one problem. Now, there is another problem. Let's see the second

another problem. Let's see the second iteration and we will see what is the issue here. Let me remove it for now.

issue here. Let me remove it for now.

Now, in second iteration is 12. Let's

see what will happen. Now, 12, right? 12

is greater than maximum. No, 12 is not greater than maximum. My maximum is what? 20, right? So, is 12 greater than

what? 20, right? So, is 12 greater than 20? No. So, it will not go inside it.

20? No. So, it will not go inside it.

But if you see my second maximum is what? One, right? This should be

what? One, right? This should be updated. Isn't it? See maximum will not

updated. Isn't it? See maximum will not be updated because maximum is 20 and second maximum is one currently. Right?

If you see second maximum is one but 12 is greater than one. Right? So my

maximum should not be updated at this point. But second maximum should be

point. But second maximum should be updated. That my second best maximum is

updated. That my second best maximum is 12. So I have to put one more condition

12. So I have to put one more condition here. L if

here. L if L if num is greater than second maximum right if my number is greater than

second maximum then I will say my second maximum is equal to num and now what will happen if it will not

go in this if condition it will call l if and check if is it 12 is greater than my second maximum which was one yes then updated right now I I have the track of

maximum and second maximum properly right and let me return these two so I'll return maximum as well as second maximum right and let me call this

function now right now remember this return will be after four so after all the iterations are over whatever the values of maximum and second maximum at

this point I want to uh print it right so let I want to get a uh I want to return it to the function

right let me call this okay so second largest number instead of that I will do this right and this is my array that I'm passing and let's run this

okay so I'm getting 40 and 30 let me clear it once and I will again run it so if you see largest is 40 and second largest is 30 right I so the function is

get second largest element I can just get this one right I I'm just showing you right that this is my maximum and second maximum in in uh question if you are getting you have to just do this

okay I hope this makes sense now there is one problem in this solution is still let's say I have another value as 40

right let's say another value as 40 so I have 40 40 times and then 30 now expectation is I should get 40 and 30 still right my expectation is the

highest number should be 40 and next second highest should be 30 right let me run this and see what will happen okay I'm getting 40 and 40 two times

right why because if you see it is checking if num is greater than second max num so when it is coming here at the last iteration right the highest element

was 40 right and second highest was 30 right till here you can imagine yeah till here now when I reach 40 it will check is Is it greater than maximum? No.

Is it greater than second maximum?

Definitely. So it will replace the value and that's the problem. So in L if I want to make sure this num is not equal to the maximum. So I'll put one more condition and num should not be equal to

maximum then I will not have problem right. So let me just remove this and

right. So let me just remove this and one more time I will run this. Now if

you see I'm getting right value 1430.

And what is the time complexity? Now

this is a for loop. We are running the loop one time over the array. Right? So

it is O now. Right? So if there are n elements, we'll be doing the N operations, right? So we again improve

operations, right? So we again improve the solution. Instead of N login, I'm

the solution. Instead of N login, I'm solving it with O sorry, O N.

Okay great.

Thing is here that we cannot hardcode this 1 and zero, right? I just put it 1 and zero so that I can explain you. But

we should in initialize it with a negative number. A big negative. So I

negative number. A big negative. So I

will say minus infinity.

Right? So anything will be greater than this. Right? So I my initialization will

this. Right? So I my initialization will be a big negative number.

So everything will be greater than this.

And I can easily find my first highest and second highest. Right? And this all fs. Okay. Great. So let's solve a next

fs. Okay. Great. So let's solve a next question. In this question um let me

question. In this question um let me first comment this my requirement is let's say I have a array right let's say this is my array nums equal to 2 3 4 2

35 I want to find first nonre repeating element

right first non-re repeating element in my list in array or list right so what does that mean if you see we will start from left first not repeating. So two is

repeating or not? Yes, two is repeating.

Two is here as well as here. Three is

repeating. Yes. Three is repeating.

Okay. Four is repeating. No, four is not repeating. And five is also not

repeating. And five is also not repeating. Right? But what I want first

repeating. Right? But what I want first non-re repeating. So my answer will be

non-re repeating. So my answer will be four. Four and five both are non-re

four. Four and five both are non-re repeating. But in array whatever comes

repeating. But in array whatever comes first out of all those non-re repeating element I have to give that. So answer

will be four. Right? So let's see how we can solve this. So one brute force method is let's say what I will do right I will create nums and let's say I will

create one more nums one right I I'm just telling you how you will do it right I mean I don't have to create it I can use it again nums two times in my

program but just to explain you so I will pick first element right and I will check if it is there or not in this right if it is there right so I will

check two I will not check the first element and I will check in remaining if it is there then I this is not a not a meeting element this is a repeating element basically so I will discard two

then again I will pick three right and I will check three if it is there or not if it is there again then this is not a non-re repeating element again I will pick four and then again I will check in

rest of the array right if I'm at index three I will check from index four from that array right similarly when I'm at five I will check going forward Right.

Okay. Great. So the problem here is the problem here is that it will be O N² time complexity.

Right? This is a brute force method. It

will be O N² because you are running a loop inside a loop. So for each element of this array you are running a loop on this array. Each element of this array

this array. Each element of this array you are running a loop on this array.

Right? So this will be a brute force method. You can solve it. get the right

method. You can solve it. get the right answer but not the efficient one. So we

will see how we can write a efficient code for this. So what we will do right let me remove it now I don't need two

arrays right as such so what I will do I will get frequency of each element first that how many times two is there how many times three is there how many times

four is there once I have that frequency then I will check from this array which is that first element whose frequency is just one right so let's go step by step

so I'll write a function um define first nonre repeating right so most of the time you have to write a function it is not that you have to write a code

directly you have to write a function always right okay so what it will take it will take a array that's it we'll get the array and then we have to find first

non-re repeating element okay so I will def in a dictionary right frequency and it will be initially empty okay great now before I write the

solution I want to tell you something about dictionary right so let's say you have a dictionary so you have a dictionary let's say frequency equal to and I will create some key value pair

let's say um uh ankit right and it's his age age right or let's say employee ID 1

and it's name let's say this is better example okay so let's say this is your dictionary now I you want to see the value of the element in this dictionary.

So what you will say you will say frequency of one right and if you just print it

it will give you okay so if I just run this uh frequency okay I have to give in double quotes or

let me make it like this.

Okay.

Okay. So it is giving me an which is correct right. Let me clear it once.

correct right. Let me clear it once.

Okay. So I'm saying uh what is the value of one frequency and I will pass the uh key value right and it will give me the value of this. So I will pass the key in this dictionary and it will give me the

value. Now if I pass some other key

value. Now if I pass some other key let's say two right now two is not there. What will

happen? It will give me error. So two is not there. Now there's a more

not there. Now there's a more sophisticated way. So instead of

sophisticated way. So instead of directly saying that give me the value of this key you can say give me frequency

dot get and you will pass the value so you will say give me the value of one right and let's see what will happen now I'll say print so get is a function I'm using a

dictionary method you can say right and let me run this so I'm again passing one and it should give me an right and this is perfectly fine right this is giving me an so there is no issue. Now

advantage here is if I pass a key which is not there it won't fail right so if I say sorry clear that so if I say let's

say two right so it will not fail it will just return none so my program is not failing right I don't want program to fail when some key is not there I'm

just saying none you can pass a argument that if it is not there then default it to something so I will default it to zero then it will give me zero right So this is a more sophisticated way. If

something is not there, it's fine. Give

me zero. Right? And if it is there, give me that value, right? So this this works fine. Okay. Now let me write the

fine. Okay. Now let me write the function. I hope you understand this.

function. I hope you understand this.

Right? So let me comment it so that I can refer it back if required. And let

me go back here and I'm going to write this. So initially I'm initializing my

this. So initially I'm initializing my dictionary frequency equal to empty dictionary. Now I will run a loop for

dictionary. Now I will run a loop for num in nums. Right? So I'm running a loop for num in nums. Now what I will do

I will say frequency. So how to add element in a dictionary? I can just pass a value num right equal to

frequency of num right. So I will get it what is the value of? So in this what I want to keep ultimately in this I want to keep the frequency of everything. So

I will say two is two times right and three is two times and four is one times

and uh uh two five is one times this is what I want to keep in this in this right so what we will do now so I'm just

checking that if it is there already then add one in this right right so if so Okay. So let me let me do this. So

so Okay. So let me let me do this. So

what I'll say I'll say frequency dot get right what you get num. If num

is there if not then make it zero + 1.

Right? So what I'm saying that check if num is there or not in this dictionary.

Right? If num is there so initially in the first loop what will happen? Right?

Num is there or not?

two is there or not in this dictionary initially it is empty right so it is not there so it will give me zero and I will increase the count by one so at this point a entry will go and it will say 2

col 1 in my dictionary right so I'll just write it in my dictionary right there will be a entry which will say 2 col 1 right this is how you have to do

it in the next station three will come right again for three right is three there no three is not there so again it will become from 0 + 1. So there will be

one more entry. It will say for three the key value pair is this. So this is key. This is value. This is key. This is

key. This is value. This is key. This is

value. Now for the third iteration again four will come and there will be one more entry. 4 col 1. Okay. Now in the

more entry. 4 col 1. Okay. Now in the next iteration what will happen? It will

two will come again. Right? Now it will check if two is there. Yeah two is there. What is the value? One. So it

there. What is the value? One. So it

will get one and plus one. So it will make it two.

Right? Again it will check if three is there or not. So three is there. Yes,

three is there. So add one. So this will become two. Next it will check five.

become two. Next it will check five.

Five is there. No. So again it will default it to 0 + 1. And there will be one more entry 5 col. Right? So let me

run this and show you. So I'll just say return return uh this dictionary. So this is my dictionary basically right and let me

call this. So I'll just say

call this. So I'll just say print and first non-re repeating character and it will take just one argument and let's

see what it returns. If you say it is returning me the same thing 22 2 3 2 4 1 5 1. So basically I get the frequency of

5 1. So basically I get the frequency of each element in this list. how many

times each element is 2 is two times, three is two times, four is one time, five is one time which is absolutely fine. Now what I have to do next? Now I

fine. Now what I have to do next? Now I

need to run a loop again on this, right?

Because I need to find the first non-re repeating element. So I will check first

repeating element. So I will check first two. What is the frequency of two? Okay,

two. What is the frequency of two? Okay,

frequency of two is two. So this cannot be the first non-re repeating element.

Then again I will check three. Okay,

three is there. Yes. And what is the frequency? Two. So, this is also non not

frequency? Two. So, this is also non not non-re repeating. Then, okay, four.

non-re repeating. Then, okay, four.

Yeah, four is one. So, this becomes my first non-re repeating element, right?

What I will do? I will say instead of returning this, right? So, what I will do? I will say

do? I will say I will come out of this, right? I got

the dictionary and I will run loop one more time. I will say for

more time. I will say for num in nums again right I I'm running the loop again if

right so I will get uh what is the count of this num right count of two if uh from frequency I will get the count of now we are sure that this number will be

there so get is not required because all the elements are going from this dictionary itself in my dictionary all the elements are going from this list itself right so I'll say if frequency of

num is equal to equal to 1 right then return it

return uh null that's it right this is what we have to do I have

to run loop again on this and I'll check if the frequency of the number right? So

frequency so the what is the frequency of two? It is two. So it will go in

of two? It is two. So it will go in this. No, nothing will happen. Then

this. No, nothing will happen. Then

second loop what is the frequency of three? Frequency of three is two. Then

three? Frequency of three is two. Then

again it will not go in this if condition. Then again four right what is

condition. Then again four right what is the frequency of four? One. Okay now it will match and it will return me that right and that's it. Now let me run this again.

So now it is giving me four. Right?

First non-re repeating element is four.

You can see right? Let me add four again here and then we will see what will happen. Now first non-re repeating

happen. Now first non-re repeating element it should give me let me add one more six right. So first non-re repeating should be five now. Right? Let

me run this and you see it is giving me five. Right? Now what is the time

five. Right? Now what is the time complexity here? Right? So if you see we

complexity here? Right? So if you see we are running loop one time on this array.

Right? And again I'm running a loop.

Right? So two times I'm running a loop.

So my time complexity will be O N and I'm running a loop one more time. It

is not loop inside loop. There are

separate loop. If it is loop inside loop then it will become O². But this is one loop. So if let's say there are 10

loop. So if let's say there are 10 elements. So there will be 10 operations

elements. So there will be 10 operations here plus 10 operations here in the worst case. Right? So there will be 20

worst case. Right? So there will be 20 operations. So it will be O 2 N or 2 O N

operations. So it will be O 2 N or 2 O N whatever you call it.

Right? So, n + n right which is better than n² right? 2 n

is better than n² n² will be 100 right.

So it will it will be double of number of elements and this is also called as linear right it is just doubling but it is not that it is uh if there are 10

elements and it becomes 100. Now this is not happening. So this is also a linear

not happening. So this is also a linear time complexity that number of operations will be linear. If there are 10 elements then operations will be 20.

If there are 100 then 200. It is linear just double. Right? So this is also

just double. Right? So this is also overall considered as O only. Right?

Okay. Great. I hope it makes sense.

Uh next let's solve uh another very famous interview question which is based on twosome problem.

Right? Very very common this is.

Now what we have to do in this? So let's

say I have a array right nums 2 7 11 15 and the target is uh 9. So what we need to find is we need to find that the

indices of those two elements in this list right let let me make it a little big this list let me make it one here

and maybe I will take two here and seven here right okay so in this list right I need to find those two indices whose

value sum is nine so for example 1 and 2 2 is three, right? Indices of those two elements whose sum is 9, right? So 1 and

2 is three. So this is not a solution. 1

and 11 is not a solution. 1 and 15 is not a solution. Right? 2 and 7 is a solution. Right? So 2 + 7 is 9. So my

solution. Right? So 2 + 7 is 9. So my

answer will be right answer will be index of these two elements. Right? So

it will be this is index one right and this is index four. 0 1 2 3 4. So answer

will be 1 and four because the sum of values at index 1 and 4 2 + 7 = 9 and this will be given that there will be exactly one solution. There will be

exactly one pair whose sum will be 9.

Right? So this is the question 1 4 or 4 1 both both are fine order doesn't matter whatever you give.

Okay. So if you have to do this with brute force again right you just imagine one more array is there right what you

will do you will pick this and then run loop over it and see 1 + 2 is 9 no 1 + 11 is 9 no 1 + 15 is 9 no 1 + 7 is 9 no

again you will check with two 2 + 11 is 9 no 2 + 15 uh 9 no 2 + 7 9 then you will return it

right but again it will be O N² right so again you should not do that right you can try solving it yourself once but that is not a optimal solution we want

to solve it so most of the problems right that you get you can easily solve using O square brute force and ultimately in your interview you have to think about how I can solve it in O

right so let's see how we can solve it and what I will do let me define the function um so am I calling Let me comment this first.

Okay. So define a function and I will set get uh to sum right. So it will take two arguments

right. So it will take two arguments right? One it will take nums the the

right? One it will take nums the the array and what is the target right? Two

things required in input.

Okay. So this is my get to some function uh signature uh array and the target.

Now how should I go about it? Right? So

what I need basically I need to check each element with every other element.

Right? Now think about it. Right? Once I

see a element I should be able to store it somewhere so that when I'm coming here so I will run a loop on this basically right. So I will check one. So

basically right. So I will check one. So

first element it is fine. Let's move to the second element. Now when I'm coming on second element right I I'll not do a double loop that I will

pick one and check in full. No I'm not doing it. I'm just checking the first

doing it. I'm just checking the first element. So I will run loop one. Now for

element. So I will run loop one. Now for

the first element nothing at least two element should be there to check. Now

when I'm at two I will store it somewhere in the dictionary that I have seen one already.

Right? If it is seen and then I will check okay that what is required right that what is required to get nine

right so what is required to get nine seven so two is there I need seven and I will store one somewhere so let's say I have a dictionary where I will store that one is there at index zero right so

this is my dictionary so after first loop I will store this let me do a command that one is there at

index zero. I will store it. Right? Now

index zero. I will store it. Right? Now

in the second one, right, I need total 9 and this is two. So 9 - 2 is 7, right? 9

- 2 is 7. So I need to check if seven was there already or not in my list, right? I would have stored it in the

right? I would have stored it in the dictionary. Seven was there. No, seven

dictionary. Seven was there. No, seven

was not there. So there is no pair till now which can give me nine.

So again I will store location of two is one in my dictionary at that point.

Great. Now again I will move forward 11.

Right? Now 9 - 11 is -2. Right? So I

will check is -2 there in my dictionary.

Minus2 is not there. Right? So I will say okay store 11 also that 11 is there at index 012.

Great. Now what I will do? I will move to 15. Right? 9 - 15 - 6, right? 9 - 15

to 15. Right? 9 - 15 - 6, right? 9 - 15 is - 6. Is it there in my dictionary?

No. So, I will store uh I will store 15.

15 is at position three. Right? Now, I

will come to 7. 9 - 7 2. I will check is two is there? Yes, two is there at index one. So, I will say this index plus this

one. So, I will say this index plus this index will be my answer. Right? this

index plus this index will be my answer and that is how I will be able to solve it right. Okay. So let let's write it.

it right. Okay. So let let's write it.

It it is easy right? If you understand this logic what we are doing right then it will be easy. You can watch this video again just go back 2 minutes back if you don't understand this logic once

you understand the logic writing it is very easy right. So I will create this dictionary let me call it seen anything that it is already there or not or

anything it is fine I'm saying see that uh this number I have already seen it in my list I'll say for

again I will run a loop num in nums okay now I will get that what I need I

need that complement what difference I need Right. Complement

equal to target. So what is my target minus this num right? Target minus this num. So for the

right? Target minus this num. So for the first iteration it will be eight. Right?

And I will see that eight is there or not? Have I seen eight already? Now this

not? Have I seen eight already? Now this

is the first element. So it will not be there obviously. Right? So for this

there obviously. Right? So for this iteration it will not be there but it is fine. Right? So I'm saying compl

fine. Right? So I'm saying compl complement equal to target minus num. So

I will get this complement and then I will check if this complement right is there in my dictionary.

Currently for the first iteration it will not be there but at some point I will get the complement right whatever complement I need. So that this number

plus uh that complement will become nine. Right? So target minus num is

nine. Right? So target minus num is complement. I will check if this

complement. I will check if this complement is there in my dictionary or not. If it is there then all I need to

not. If it is there then all I need to do is return right return that the index of this

right now here I don't have a index right if you see I'm just checking num so it will give me what it will give me 1 to 11 I need a index also so I can say

index comma and I will say enumerate what enamel will do it will give me index as well as the value. Right? Maybe I can

show you quickly. Uh index,

right? Let me just uh comment it for now. I I want to show you what it gives

now. I I want to show you what it gives me. Right? So, I'll say print

me. Right? So, I'll say print index num.

Right? And I will just call this function get to sum.

Okay. So I'll say print get to sum.

Okay. And let's run this. Oh, I can just call it because we are printing it inside. That's fine.

inside. That's fine.

Okay.

So it is giving me 1 12157. You see 1 2 157. But it is giving me the index as

157. But it is giving me the index as well. 0 1 2 3 4. Okay. I hope you got

well. 0 1 2 3 4. Okay. I hope you got it. Okay. So let me come back to the

it. Okay. So let me come back to the question again and let me remove it for now. Right? So

I'm saying if this is there I have already seen this difference. Right? I need target

this difference. Right? I need target minus now. So if it is if it is one

minus now. So if it is if it is one right? If I have seen I have eight here

right? If I have seen I have eight here in this then we are good. The sum 1 + 8 will be 9. Right? But for the first iteration it will not be there. But it

was there then I could have said return index right and the index of the value which I

have seen somewhere right and for that I have to say seen and the index of that complement right where what index that complement is

present right I'll do the iteration and show you right great now it is fine if I find it it is Great. I will return it and that's it. But what if it is not

there? Then I have to add that into this

there? Then I have to add that into this dictionary. Then I will say scene right

dictionary. Then I will say scene right that value which is num right I will store this num and the value will be the

index of that right great. So I'll run it and take you through again. I know it is little little confusing but it will

be clear. So I'll just say uh print

be clear. So I'll just say uh print get to sum right print get to sum nums target let's

run this so see I'm getting four and 1 4 and 1 means four is 0 1 2 3 4 right 7 and index 1 is two so 7 + 2 9 right let

me add one more number in this so if I add let's say 10 and if I run it again right I'm getting five one right I can add a element later

as well let's say 15 or let's say 20 right so still it is 5a 1 okay so this is clearly fine right now what I'll do

I'll just run over the iteration one more time so that it is absolutely clear to right so what I will do here I will

just say print um uh print scene and print num and scene these two things I'm going to print it right then it will be absolutely clear how these these

things is working okay so if you see for the first iteration my number is what let me take the array where is my array

uh okay this is my array okay fine let me okay so for the first iteration this num is one Right? Num is one and

what is there in my scene? 1 col 0.

Right? Because there was no complement present in my set. Right? Initially set

was empty. So it didn't go in this.

Right? And I added this value one and the index of it. So the one and the index of now in the second iteration two

right again I'm checking that 9 - 2 7.

Right? My target is I should find a seven. Do I have seven in this

seven. Do I have seven in this dictionary? No. Seven is not there in

dictionary? No. Seven is not there in this dictionary. So again it will not go

this dictionary. So again it will not go inside this. Right? Again I will just

inside this. Right? Again I will just add this value and I will say there is a two available and the position is one.

The index position right now next 11.

Right? Again 11 9 - 11 -2 is minus2 there in my dictionary. No it is not there. So it will add that value again.

there. So it will add that value again.

And it will keep going on like that.

Right? So till here it will not find anything. Right? So it is keep on adding

anything. Right? So it is keep on adding keep on adding keep on adding. Now when

it reaches seven right so it will check what is the complement target minus num.

So 9 minus 7 2. If two is there yeah two is there. So the moment two is there it

is there. So the moment two is there it will just return this value. It will

just return value and function call will be over. That's it. The moment you say

be over. That's it. The moment you say return your function is done and it will return that value right and that is how it is returning me 5 col

right so I'm returning the index of this element which I'm checking currently which is seven and the index of the the complement right so um seven is there I

need the index of two and which I'm getting from here the index of two is one so I'm getting five and okay so these things comes with practice

right you should solve yourself once no matter how many videos you see if you are not able to solve it yourself uh uh when you practice then you will not be able to solve this in interview as well

okay so make sure you practice it just in your mind first think about it how should I solve it what is the uh algorithm I should do right and then

after that you should start writing the code okay great let's move to the next question and in this question we will introduce a new data structure which is set and I will talk about set right so

the next question is let's say you have a list right let me comment it so you have a list let's say this is your list and you want to find all the

elements which are duplicate so what it should return it should return all the elements so it will return like uh one so one is duplicate right one is there

one is there as well then two is there yeah two is also there right duplicate Three is duplicate. No. Four is

duplicate. No. Five is duplicate. No. So

we should get one or two. Again the

brute force is you have one. You will do a loop inside loop. Right? And you will just check if one is there again you will add it into this list right and two

is there then you will add it into a list. So you will create a empty list

list. So you will create a empty list and you will keep checking wherever duplicate is there you will keep adding it. But again it will be O² right? So

it. But again it will be O² right? So

let's see how we can solve it with a optimized O method.

Okay. Now before I start the solution of this problem, I want to talk about set.

Right? Set is another data structure. So

you declare it as let's say um let's say variable name is scene and you say set.

Right? Now in this you can add elements.

You can just see dot add you will use right. For list you have to say append

right. For list you have to say append but for this you have to say scene dot add right this is a variable name which I declared as set and you can add

something let's say you can add one and let's say you are adding two and if I just say print

scene right and if I print it we will get a set uh it is in curly braces similar to dictionary but it is not a key value pair it is just individual element

Okay. Now there are two properties of

Okay. Now there are two properties of it. One is it cannot have duplicate. So

it. One is it cannot have duplicate. So

if I again add let's say two, it will not store it. So if I just do this see again one right in list if you add it will just add it right. It it doesn't

have any duplicate issue but in in uh set you cannot have duplicate. Second

main advantage is the lookup is just like a dictionary. Right? If you're

looking a value this is used for the lookup basically right if you want to check if some value is present in not it's there or not then it takes one time complexity right if you do that in

dictionary that will be o for example if I just say print sorry print

uh one in scene so I'm checking if one is there in this scene set or not right let me clear it quickly Clear.

Uh okay.

I think we went into Python environment. Okay. So, uh, uh,

Python environment. Okay. So, uh, uh, we'll say, uh, yeah, I'll just run this.

Okay. So, I'm getting 1,2 where I'm getting this uh one. Okay, I'm printing this fine. Okay,

one. Okay, I'm printing this fine. Okay,

so I'm checking if one is there in scene or not. Yes, it is there. So, it is

or not. Yes, it is there. So, it is returning me two. two is there in my scene. Yeah, two is also there. It will

scene. Yeah, two is also there. It will

return two. Three is there. Yeah, it is not there. So, it will return me false.

not there. So, it will return me false.

And this lookup is one time complexity not. If I'm looking this in a

not. If I'm looking this in a dictionary, so if I'm checking three in nums, if three is there in nums or not, right? This is a time complexity, right?

right? This is a time complexity, right?

Because for that I need to run a loop over it. But for set similar to

over it. But for set similar to dictionary the hash values are stored.

So it will just take 01 time complexity.

Right? So this is how the set are beneficial. If you want to do a lookup,

beneficial. If you want to do a lookup, you want to store some values and you want to do a lookup you can use it.

Right? You can use dictionary as well.

But dictionaries you use when you need key value pair both you want to store.

If you just want to store one value not key value pair then sets are better.

Okay great. Now let's go back to the question right I want to find all the duplicates so I'll say define find duplicates

right and this will be nums right so this is this is all I need okay so I will define a set scene equal to set

right what I'll do I'll just tell you and I will say dupes equal to a list so whatever duplicates I will find I I will

keep in this list I will keep appending it right we'll optimize optimize it further but for now I'm keeping doses right

and whatever elements I have seen I will keep in set right what do I mean by that so I'll run a loop for num in nums

right and I will check that this element is there in scene or not right if it is there then it is a duplicate if If it is not there then we are good right. So

what it will say if num in scene right if num whatever element one if it

is there in my set already so I will append it to dupes right because it is a duplicate it means right dupes u dot append

that element now okay if it is not there then what I'll say I will add add it to I will add it to my scene. So I'll say

scene dot add and I will just add that element now. Right? So in the first

element now. Right? So in the first iteration what will happen? So one it will check if one is there no. So what

it will do it will add that in the C right again it will go two right it will check if it is there no then it will go

to two and right I'll show you so I'll just print it here so I will say print scene and dupes both I will print and

then it will be absolutely clear right.

So let me just call this function.

Uh also I need to return it, right? So

I'll return it later. That's fine. So

I'll say print or I'll just call this function. That's

it. Right?

Okay. So I'll just call this. And now if you see in the first iteration in my set one is going right. In doses nothing is there. Right? Dopes nothing is there.

there. Right? Dopes nothing is there.

The second iteration you see again there are two elements in my uh this set that I declared right and nothing is there again three right now when it goes to

fourth iteration two right now two is there in my set yes two is there in my set it means two is a duplicate so it

will just add two here in my dup uh list right next again it will say four four is there no so it will four add four to

set right and all this lookup is 01 and then it will see one is also there and it will add one also so this is how it is working so all I need to do here is return

return uh I need to return all my duplicates there is still one problem so we'll discuss so I'll just say return duplicates then I need to predict it

right to see right so if you see it is returning Uh nothing uh

something is wrong. Okay. Sorry. I need

to come out of loop. Yeah. Yeah. So let

me run this. And now we are getting two and one. So these two values are

and one. So these two values are duplicate.

Right. So once this for loop is over then only I need to return it. Now there

is one problem. Let me add one more one.

Now what is the expectation? I should

still get two and one. I should not get 21 one right because one is a duplicate doesn't matter how many occurrences are there even if three occurrences are there in my output I want to see only

once but if I run this it is 21 one because again it is checking here right if it is there yeah one is there so it will add in this uh list right so I'm

getting 211 how to solve this so again instead of so we know in the sets we cannot have duplicates so this dups also we can create it as set instead of a list

if duplicate is there it won't go inside that I'll set dupes equal to set and then I can just um instead of append I have to say add

and that should solve the problem now I'm getting one right so this is how you have to think about it as I said again if you see we are running loop only once

right so it is again a time complexity so most of the questions are around this that it is O N² it can be easily solved

or with sorting N log N it can easily be sort solved but you have to bring it to O N right 95% of the questions are like

this right from O N² o or O N log N you have to bring it to O right so if you can think like this you are you can easily solve this kind of problem

let's move on to the next question and very interesting question this So here's the question right the question name is best time to buy and sell stocks right given the prices of a

stock on different days find the maximum profit you can get you can buy once and also sell once okay

so let's say you have the prices of a stock right in a list right this is day one price day two price day three price and so on right you have to find that

what is the maximum preference that you can get. Now two thing is one is you can

can get. Now two thing is one is you can only buy once at any price and sell once. Second thing is you should buy

once. Second thing is you should buy first and then sell first. Right? So for

example if you buy here right then you can sell any day after this. Right? It

is not that you will say I will buy at one and sell it seven. No first it should be buy and then it should be sell. Right? So let's see what will be

sell. Right? So let's see what will be the answer in this case. Right? So if

you see if you buy at seven seven is highest price so it will you will never get any profit right? If you buy at one and sell at five then you will get

profit four right again if you see maximize will be here if I buy at one and sell it six it will be profit five.

So answer will be five. You can try all the combination but ultimately the profit will be five. Right? Now if you have a array where price are always

decreasing for example 7 5 4 3 then you can never get any profit. The maximum

profit you can get is zero. You can buy at 7 and sell at 7 on the same day because it is always decreasing. You

cannot you can never get any profit here. Right? Okay. Now if I have to

here. Right? Okay. Now if I have to solve with brute force right what I will do I will run a loop on the array and compare with each price. So I will

compare seven with all the other price.

So I will say I will buy at 7 and sell at one. What is the profit? Min - 6 - 5

at one. What is the profit? Min - 6 - 5 sorry - 6 right again buy at 7 sell at five buy at 7 sell at three. So I will get all the combination and see what

will be the profits possible. Again I

will buy at one and see with every index other than this right what is the profit that I I can get and I will store all the profits and keep the maximum one

again. So I can run this on the all the

again. So I can run this on the all the elements and compare all the combination right how much profit I can get but again it will be O N² right the time

complexity will be order of O N² right which is not again good so we want to bring it to O N right as I said most of the time we'll be doing this brute

force will look like O² easy and then there will be some trick to get to O okay and it comes practice. Don't don't

think that uh if you are not able to solve these question at once you you will not be able to crack interview. No,

it is like if you solve some 50 or 100 questions in your interview, same patterns will keep on coming, right? So,

so practice a lot. Uh you can practice on namastasql.com as well. I will show you later. But for now, uh let's solve

you later. But for now, uh let's solve this question. Okay. So what we will do

this question. Okay. So what we will do right if you think about it we need to keep track of our profit right and the

minimum price right so what we will do we will start from the first element right so we will start from here okay

let's assume I'm buying here right so my buy price is seven let me write buy price is seven on the first day Okay, when I go to the next day, right,

I will check if the price is less or more, right? So, initially the minimum

more, right? So, initially the minimum price I have is seven, right? I bought

at seven. So, initially the minimum price is seven, right? First day doesn't matter. Okay. Now, when I move to second

matter. Okay. Now, when I move to second day, right, this is the minimum price now. So, what I will do? Okay. So, there

now. So, what I will do? Okay. So, there

is no point buying here. If the next price is less than this, then why not buy at this right? When you buy at less price, you will always there are more

chances of getting a profit. Right? So

when I'm moving to next index value, I mean next element, I will check if the next value is less than current value, then I update my minimum price. So my

minimum price will be one.

Okay, great. Now again I will move. Now

this time I will see okay price are increasing right so I will check what will be my profit right at this point my

profit will be 5 minus one so maximum I can get four profit once I reach day three right in day three maximum profit I can get is whatever minimum price I

could have bought it and I can sell here so profit I could have got four so I will keep this profit as four and in future we will see if there is a chance of getting more profit. Right? If

there's a chance then we will use that combination.

Okay. So this is fine. Now again I will move to three. Now three is less. Right?

Three is less which means which means if it is less. So if I do three minus minimum price 1 it will be two. So we

won't sell this right. The better option was buy at once sell at five. and I will not update my minimum price also right because minimum price was one I if I had

a opportunity to buy at one I will buy at three so in this case nothing will happen as such right okay great now let's move on to the next one next is

six right now I will check is six right is greater than my minimum price yes it is greater right so I will check five

the maximum profit I can get is six minus my minimum price five so now my profit will become five right I will check the previous maximum profit was

four but now I can achieve 6 - 1 5 right and again uh now it is reducing it is uh six and then it is four so again it is

more than my minimum price and if I check the profit 4 minus one will be three so again it will be less so my maximum Maximum profit will be five at any point right so this is how I will

solve it and let me write the code for this okay so I'll keep it as it is this so I will say define get max

and it will take an array so let me give prices itself you can say as anything is fine right so

let me keep it as it is okay so now I need to keep track of two things. One is what is the maximum

things. One is what is the maximum profit I have right? so that I can in this variable I will always keep the maximum profit possible right and in this I will keep the minimum price at

the price at which I should have bought right okay so I'll define two variables and I'll say profit

equal to zero and max profit possible uh sorry max profit possible is zero and the minimum price

or minimum buy price whatever right minimum price let's say the first element prices of zero right so we will start

with seven right I I can keep it as a infinity as well so I can say float of infinity some big number right so that when I compare I always have a lesser

because I want to keep the minimum I uh as a track so I can keep a big value and when I compare with the first value it will be less and I will be able to

replace it but this is also fine if we can take the first element okay now let's start the loop so we will iterate

over this right so I for price in prices okay now what I will check I will check if this price

right is less than my minimum price that's what I have to keep I need to keep the minimum price at which I and buy the stock. So I will say if minimum

price is less than or if price whatever this current price

right is less than minimum price right then I will just update my minimum price right because new minimum price will be this now right I'll I'll go

through the iteration so I will say then minimum price will be equal to price Right? So I'm

every time I'm going left to right from day 1, day 2, day three, I'm just checking what is the minimum price I could have bought it. That is one thing.

Now second thing at every iteration I also need to check the profit, right? So

that after every iteration I store the maximum profit in this variable, right?

And in return I will return this max profit, right? So I will calculate my

profit, right? So I will calculate my profit. My profit will be at current

profit. My profit will be at current iteration whatever is the current price minus whatever minimum price I had I

could have bought at right so that's why I'm keeping the minimum price so that I can always compare the current price with whatever minimum price in past I could have bought the stock at right and

then what I have to do I need to again check that if this profit is more or the earlier profit I was I stored that is Right? I will compare and keep the max

Right? I will compare and keep the max profit again. So I will say that find

profit again. So I will say that find the max of the current profit right as per the current price if I sell it on the current day as per the loop running

and the maximum profit I already had stored right that I will keep in the max profit. So I will update the max profit

profit. So I will update the max profit value to max of profit and max profit right I'll just print everything and then it will be clear. So I will say print.

So I I'll print three things. One is um I will keep u u minimum price right and then um

price right and profit.

Okay. So these three things I will print and then it will be clear. So I'll just say get max prices.

I'll just call this.

Okay. Now it will be very clear to you.

So what I'm saying initially the minimum price is this. Right? Let me take this here. It will be more intuitive.

here. It will be more intuitive.

Okay. So minimum price initially is seven. Uh the first element, right? This

seven. Uh the first element, right? This

is my first element. Seven doesn't

matter. And again and then I have the price. So in the first iteration price

price. So in the first iteration price is also seven. So profit will be zero.

Right? In first iteration nothing much.

What I can max do? I can buy at this price, sell at this price and profit will be zero. Now let's move on to the second iteration. Right? In second

second iteration. Right? In second

iteration if you see this is one right or let me put price first and then uh minimum price. Right? Let me just say this

just wait a second. So this is my price and this is my minimum price. Right?

Okay. So let me run this again.

Okay. So now my price is one. Right? Now

I'm getting a price of one. Okay. Now I

will check if this price is less than my minimum price. Right? So my minimum

minimum price. Right? So my minimum price was seven earlier. Right. Second

second variable is a minimum price right? Yes, it is less. So I will update

right? Yes, it is less. So I will update the my minimum price value to one right.

My minimum price value will be one now.

Right? Now what will be the profit?

Profit will be uh price minus minimum price. Right? So that will be again

price. Right? So that will be again zero. Right? So I will say okay maximum

zero. Right? So I will say okay maximum what I can do is I can buy at this and sell at this. Right? If I buy at 7 and sell at 1, it will be min - 6. So I

updated my minimum price and the max we can do is 1 - 1 0.

Okay. Now let's move on to the third iteration.

Five is five.

So if your price is less than minimum price. No. Oh, sorry.

price. No. Oh, sorry.

Wait. Okay. Yeah. So if my price is less than minimum price, no, my minimum price was one. So I will not update my minimum

was one. So I will not update my minimum price now. Right? My minimum price is

price now. Right? My minimum price is still one. But now I can get the maximum

still one. But now I can get the maximum profit of four. So I will update my profit to earlier it was zero. Now it

become four.

So at this point I can get a maximum profit of four. Right? Then again I will move the next element is three. Right?

Now is three less than my minimum price?

No. Three is still greater. So I will keep my minimum price one only. Right?

Now what will be the profit with if I sell today? So three minus minimum price

sell today? So three minus minimum price two. Right? Still I will not update my

two. Right? Still I will not update my max profit because earlier I could have got four. Why I will sell at three?

got four. Why I will sell at three?

Right? Okay. I hope this is making sense. Right? Now next is six. Is it

sense. Right? Now next is six. Is it

greater than my uh is it less than my minimum price? No. So my minimum price

minimum price? No. So my minimum price stays. But now my profit will be 6 - 1 5

stays. But now my profit will be 6 - 1 5 right. Which means now I will update my

right. Which means now I will update my profit to five. Earlier it was four. So

it would have done 4, 5. So it will get me five. Right? So at every step I'm

me five. Right? So at every step I'm keeping track of minimum at what point I I could have bought the stock. And I am keeping track of profit as well that if

I sell today as per the minimum price how much profit I could have got right and in the end I will return this. So

basically you just have to say return max profit here.

Return max profit, right? And I'll just print this.

right? And I'll just print this.

And we are getting uh zero uh uh what is wrong here? Let me clear it.

Okay, it is giving me zero.

Okay, so uh the problem here is this return has to be outside the for loop.

So what is happening is after the first iteration only we are just returning, right? So when the for loop is running

right? So when the for loop is running after first it in the first iteration we are just saying return right. So it is giving me zero right? So I have to check after all iteration what is my max

profit and then I should return it.

Okay, now I'm getting five.

Okay. So these kind of mistakes will happen initially but as you practice uh you'll be very comfortable with all of these things. We have to keep the return

these things. We have to keep the return and how to bring solution from O² to O.

Great. Next let's move on to the next question. Next question uh we will solve

question. Next question uh we will solve on parsing logs. Right? These are very common questions um in data engineering specifically. Right? So let's say you

specifically. Right? So let's say you have some logs of users. Right? Uh this

is how your logs are. It is a list and it has some text right in some particular format and you need to get an output on each date how many error each

user has got. For example on first right this is my first data. So if you see user one has got error only once. So

this is info. Info is fine. We don't

have to count. We need to count only error. So user one has got error once.

error. So user one has got error once.

So if you see on 1st of um January 2024 user one is one and user two has got error two times you see on the first.

So this is what you have to get again on second if you see on second there is only uh user one has got error two times right and user two is not there because

there is only one info right so let's see how we can solve these kind of questions okay so what we will do here is first we

need to read each of this element from the list and split it so that we can get all this in a different variable this date,

error level, username, right? So these

things we need. So let me first show you how do we get it by using one element only, right? Uh let me just comment it

only, right? Uh let me just comment it out.

Okay. So I will take log zero for now.

Log zero.

Right? So this is my log for now. I'm

just taking one just to explain how this works.

Okay. Now I will say parts equal to log dotsplit.

Right? And I'll see what it will give me. Print parts.

me. Print parts.

I'll run a loop later. But for now, I just want to show you how to extract all of this. So I'm splitting. When I say

of this. So I'm splitting. When I say split, by default, it will split on white space. Right? If you have comma,

white space. Right? If you have comma, you can give comma here. That is split based on comma. Now once I have this information all I need to do is that okay give me the date. So date is

nothing but the first element of this list. So I will say date or let's say

list. So I will say date or let's say log date equal to log date equal to the first

element. So parts zero right now if you

element. So parts zero right now if you see I will get this full date including time stamp. I need just the first 10

time stamp. I need just the first 10 characters so that I just get the date because I need date wise. I don't care the time stamp. Right? So further I can

say give me all the elements from zero till 9. So 10 elements it will give me

till 9. So 10 elements it will give me right. Let me print it quickly.

right. Let me print it quickly.

Okay. So you see I'm getting the proper date. Great. The next thing we need is

date. Great. The next thing we need is what is the error level? Right. So label

is is it error? Is it info because we are interested only in error right? So

I'll say uh let's say label equal to uh so it will be the 01 first

part right so I'll say parts one right and let me just print it you see I'm getting this info as well

the last thing I need is user so I will say user user equal to parts two and let me print user as well.

You see so we have got date we have got the error level and user these three things we need right and just we need to count it okay so let me convert this to

a for loop so I'll say for instead of taking a first value I'll say log in logs

right and then I will just remove it and I'll just comment it out okay great I'll just uh give indentation and we should be good. Let

me run this so that we see for the all the logs entries.

Yeah. So we are getting this right. Now

in data engineering lot of time you are you are using some API or you have some logs in JSON format in a file and you have to work on it right. So this is very common task in data engineering.

Okay. Now what we will do? We need to now do this count right. So for that uh let me create a function first right we

will write a function. So like Ctrl X and I'll say define uh logs summary

right and input will be logs right and then I will start writing my code here right let me put indentation

yeah okay so now what we will do we will uh uh declare a dictionary uh where we will give the result right initially it is empty Right? This is my dictionary

basically. So I'm initializing it with

basically. So I'm initializing it with the empty dictionary. Okay. Now first

thing that we need to do right let me comment this that now when we have to make a entry in this dictionary right only when there is

a error not in case of info right so I will say if label equal to equal to error

then I will do some task otherwise I'm going to ignore everything right if it is error then I will do something okay Now if it is error now what we need to

do now in dictionary we have to have this date right if date is already there I need to update just these values but if date is not there in my dictionary

right I need to first insert a record with that date so I'll say if log date not in my dictionary

right then make an entry so I will say result log date

uh sorry result log date right so this uh date will go with the empty dictionary right so basically this

will go in the first iteration and there will be empty dictionary at at that point right so I I have only so after first iteration I will have something like

So like this it will be and this will be empty right now when next time now I will start

adding these uh key value pair in my dictionary later but basically this is what we will have after the first iteration.

Okay now this is done. Now next what I need to check I need to check if whatever user is coming right if there is a entry for that already I will do a

plus one right in that date only otherwise I will make entry that this one uh uh error right so I'll say result

right so this is a dictionary now right and within this I need to update for the particular user right so I will use get so I will say result

log date. I have discussed this before,

log date. I have discussed this before, right? That if it is not there, it will

right? That if it is not there, it will return none and we can default it to zero. And I will get this user is there

zero. And I will get this user is there or not. If this user is not there, I

or not. If this user is not there, I will default it to one. If it is there, then whatever value we will get, we will do + one. Right? And that will do for

me. Okay? So, result log.get user + one.

me. Okay? So, result log.get user + one.

And let's see what is my uh return result right and let me just call this print

uh log summary. Yeah,

I think we have got the answer. Let me

just check this.

Yeah, I think it looks fine. So on first user two has two and user one has one and on second user one has two right.

This is the I think expected output.

Yeah. Okay. I hope this makes sense and this is very common thing in data engineering when you are working apart from interview as well. This is a very common thing. You will be working with

common thing. You will be working with dictionary list you will be passing it running a loop. So you should be very comfortable with these kind of problems. Okay. So if you want to practice more

Okay. So if you want to practice more problems right I was telling you that you can practice more problems right so you can go to namastasql.com right you can go to coding problems so we have lot of different category of

problems here analytics python coding ETL so you can just click on uh python coding and you can solve the questions right mostly questions are on python codings

are free right so you don't have to buy anything they are around 25 30 questions so you can get a good hang of uh the things how how questions will be asked and after that maybe you can practice

more on lead code easy questions and maximum medium questions right not more than that right so if you practice for example if you practice 100 problems right then that will be enough that will

be enough for you um to solve any kind of problem during the interviews okay I hope this this video helps please do like the video because it takes lot of effort to create these kind of videos

right subscribe to the channel. If you

have not subscribed to the channel and if you are looking to learn Python from start, you can just go to courses and you can buy this course and it is very fantastic course right you will have end

to end understanding of Python how it is used as a data analyst or data engineer or even data scientist right and you will build a very good foundation because we have started from scratch

right so there is no prerequisite as such right you can just come and start learning the Python okay great thank you everyone Have a good day. Make sure you like the

video and if you have not subscribed to the channel, please subscribe.

Loading...

Loading video analysis...