Python Practice Interview Problems - ACE a Python Interview! Practice your coding skills - Episode 1

LeMaster Tech
183 followers
4 Video Views·Apr 23, 2025
Prepare for a technical interview for a Python-related Job with these sample code problems!
This is episode 1 in a Python practice problem series coming from LeMaster Tech.
Be sure to give us a like, drop a comment, or subscribe to the channel for more content like this!
The previous video:
https://www.youtube.com/watch?v=nyrbv7AuWIk&list=PLsFyHm8kJsx1A_KgUcsyRyX99pd6NOc50&index=7
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
today's video will be part one in a
series of practice problems preparing
you for
python interview questions or tests or
quizzes
um this is going to cover a lot of
practice scenarios and writing python
code but we're not going to focus as
much on
the tutorials or basic syntax of python
so if you're unfamiliar with python in
general there's a on this channel
there's a introduction to python
tutorial series i would recommend
starting with before diving into these
problems i'll leave a link to that
because for this series we'll be
assuming you have some basic knowledge
of python so without any other further
delay let's jump right into it problem
one
the parameter weekend is true if it is a
weekend saturday or sunday the parameter
vacation is true if we are on vacation
we sleep in if it is a weekend or we're
on vacation return true if we sleep in
so i suggest pausing the video at this
point if you'd like to attempt it on
your own if not i'll take about a one
second break then i'll start talking
about solutions to solve the problem so
i recommend pausing now so let's go
ahead and take a look at how we'd solve
this
so right away let's go ahead and define
a function
and we'll call it sleep in
this is going to receive two parameters
defined as weekend
and vacation
and these parameters are going to be
booleans these are going to be true
false parameters but you actually don't
have to define that anywhere it's just
how it gets used inside the code
so what we're saying is if it is a
weekend
or
if we're on vacation
then we want to return
true telling us
sleep in remember return true if we
sleep in
and then any other scenario meaning it's
not a weekend and we're not on vacation
we want to return false meaning we don't
get to sleep in
and this is really all we need um the
one thing about booleans this is the
same as saying if the weekend is equal
to
true or vacation
equal to true
the the this is just
the exact same statement as just saying
if weekend because these true false
conditions inside python these booleans
you can just say equals or not equals to
with if and if not so that's just one
little handy shortcut for python but no
if you had equals true in there that
would run just the same
this is just a little faster and a
little cleaner
and so to actually test that our code
works you don't just have to take my
word for it we can go ahead and we can
test every scenario so this is fairly
easy because we just have two true false
parameters
but we'll actually print all four
scenarios we could have for this problem
so
let's say they're both true
and let's say weekend is false and then
vacation is false
and then they're both false
and now if we run this we can actually
look at the results and make oh i erased
one
that's unfortunate
and if we look at the results that we
get from here we can actually check and
make sure the scenarios make sense so
let's go ahead and run this
and take a look here so what we've got
is scenario one is the weekend and we're
on vacation we return true meaning we
sleep in
if it's not a weekend but we are on
vacation we return true meaning we sleep
in
if it is a weekend but we're not on
vacation we still return true meaning we
sleep in so here we're covering it's
just a weekend it's just vacation it's a
weekend and vacation and on in all of
those scenarios we want to sleep in the
only scenario where we're not getting to
sleep in is if it's not a weekend and
we're not on vacation and that's what
gives us false
so right here just in just in three
four lines of code inside a function
we've created a a
function that handles every scenario for
this
so that's problem one
problem two given two integers return
the sum
unless the integers are the same then
return double the sum
so this is fairly similar in style to
how the previous one was done just with
a little unique twist so again if you'd
like to solve before going through the
solution together i recommend pausing
the video now
otherwise let's go ahead and take a look
at how we'd write this code
so again we're going to create a
function that's going to check whether
or not the integers are the same
and determines what to return based off
of that so we'll call that sum
or
double
and it's going to receive two integers
we'll just call a and b you can really
call the variables anything you'd like
so what we're going to do is we're going
to create a variable called the sum and
we're going to add a and b together
and then we do an additional check to
see
make sure you get your spacing right
then we do another check and we check if
a
and b are equivalent
and if they are then we're just going to
take that same sum value and we're going
to multiply it times two
and it so this way outside of the if
statement we return that sum variable
and now you can see if a and b are any
two integers that aren't equivalent we
just add them together and return that
but then we check after adding them
together if they equal each other and if
they do we just go ahead and multiply it
times two and we still return that same
variable
okay so now let's go ahead and test our
code
with a few sample scenarios where we run
our function sum or
double
and we'll give it
a one and a three where they're not the
same value
and we'll give it
one where they are the same value
we'll just say sixes so what we would
expect based on our function would be
one and three return four six and six
would actually return 24 because it's
going to do the sum and then double it
so let's go ahead and see 4 and 24.
and there you have it i mean you can
play around with this all you want i if
you write the code on your own i
recommend testing it with a wide variety
of variables just to make sure you don't
break it
but you can pretty quickly tell just by
testing both scenarios whether or not
your code is working and there's problem
two
problem three
given two integers and a boolean
positive condition return true if one of
the variables is positive unless the
positive condition is true then only
return true if both integers are
positive
so to put that another way you're going
to be given two numbers and if your
positive condition is false return true
as long as 1 is positive
if the positive condition is true both
numbers have to be positive
i recommend pausing the video now if
you'd like to attempt this before we
take a look at the solution together
okay so let's look at the solution for
this problem it's going to start like
the previous ones we're going to begin
defining a function
that pulls in all of our values so we'll
call this
is
positive
and this is going to take
two integers just like the previous
example but then it's also going to take
an additional condition we defined as
positive
and inside this code we're going to
start by checking to see if
positive is true
and if it is
then you're going to return
whatever happens by checking if
a is greater than zero
and b
is greater than zero
so that this is a shorthand way of
writing
what i'll go ahead and show in another
way after i've written this code the
first time
but first we check if it's positive and
then if it's not
we get to return
if a is greater than zero
or
b is greater than zero
so if you think through the criteria we
were given in the in the
code
we're checking to see if it is positive
a and b have to be greater than zero if
those are both true this is going to
return true but if it's
not true that positive is is is active
then we can return true as long as a or
b is greater than zero
so just like in previous examples we'll
just go ahead and print
but if we have one
negative three
and
false
then we should get a true for that
and then oh well you do have to call the
function
i'll go ahead and copy this
and then we'll do one where they're both
negative and that should give us false
and then we'll go ahead and do one
where positive is true
and we have one of each and then we'll
go ahead
and do one where
they're both positive and positive is
true so if we go ahead and run this
you're gonna see when positive is false
and one value is positive we get true
but otherwise we get false because
they're both negative
but then when positive is true even
though we have one value that's positive
we have a negative three so it returns
false and then when both values are
positive and positive is true that's the
condition where we get true
so hopefully that makes sense
one additional way this is kind of an
interesting way to write it it uses the
fewest lines of code possible
but this can confuse some people
realizing that what you're actually
doing is creating a boolean condition
inside your return statement
and maybe an easier way to think about
this
is if you create a variable called
solution
and you set that equal to your condition
so we would say well solution is a
boolean variable checking to see if a
and b are positive and then what we want
to do is return the solution
spelled correctly
and you could do the exact same thing
here where you could say the solution is
our or statement
and
i think i messed it up
a greater than zero or b
greater than zero
and then we again are going to return
the solution and what you should see
when we run this if i haven't made a
mistake yeah you get the exact same you
get the exact same answers as before
because what all we were doing is
checking a multi-variable boolean
condition inside our return statement
python is smart enough to handle that
but it's exactly the same as if you use
an intermediate variable to store the
solution
so that's two ways to look at this
problem
and that's going to do it for practice
problems video one if you found these
useful and would like me to cover more
in the future please leave a like and a
subscribe let me know there's interest
in the channel and feel free to drop a
comment asking me to cover something
different in the future i'll be doing a
number of these on progressively more
complex topics so if you found it useful
you want something else specific cover
be sure to let me know and thanks for
checking out the channel thanks bye
