Python Practice Problems - ACE a Python Interview! Medium Logic and Looping Functions - Episode 3

LeMaster Tech
183 followers
Apr 23, 2025
Prepare for a technical interview for a Python-related Job or upcoming test or quiz with these sample code problems! This episode covers intermediate logical processing and looping alongside and, if, else, or statements inside functions!
This is episode 3 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=O5kxXuqF4VI&t=128s
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
this will be video three in our python
tutorial for practice problems for
interviews and today begins the portion
of kind of intermediate difficulty
problems with some more advanced loops
and logic
again
similar to last two videos we're going
to assume you know a lot of basic python
syntax so if
you're unfamiliar with the very basics
of python i recommend starting with a
very intro to python tutorial course you
can find one on this channel or i know
i've seen others all over youtube so
i'll leave a link to that
and without further ado let's get into
the problems
problem seven
write code to determine what penalty if
any a speeding car should pay given the
cars stop the stopped car's speed as an
integer as well as a boolean true false
parameter saying whether or not the cop
likes you determine the outcome
if speed is under 45 no punishment
return a zero
if speed is between 46 and 60 including
60 small ticket return a 1. if speed is
61 or greater large ticket return a two
unless the cop likes you and then add 10
to the speed conditions for all
scenarios so 45 becomes 55 46 becomes
fif 56 etc
so you're going to be given a speed
parameter and whether or not the cop
likes you as a boolean and you return
which one of those three scenarios um
the payment is gonna be
and so if you'd like to solve this
before we go through it together i
recommend pausing the video now taking a
crack at it
otherwise we're gonna go ahead and look
at the solution together
so for this problem we're going to start
by defining
a function which i'll call
caught speeding
and the caught speeding function is
going to take in the speed and then a
parameter that i will call
likes me whether or not the cop likes
you
and let's just start by saying if he
does like you all of these checkers need
to be a little bit higher so if the cop
likes you
then we're saying if the speed is less
than
right 55 becomes our it's actually going
to be a less than or equal to
so if the speed is less than or equal to
55 we can just return a zero because
there's gonna be no punishment
the next is we can check a range of
speed and we can say well if the speed
is
okay greater than 55
but less than or equal to
70
then we're gonna return a one right
because now we're saying for scenarios
where the cop likes you and so this
defined 46 to 60 which is the small
ticket becomes
56 to 60. so
the reason we're just doing greater than
55 is that actually starts at 56. this
is the same code as writing a less a as
writing this
and this is also worth noting this is
the same code as well as saying if the
speed
is greater than or equal to 56
and the speed is less than or equal to
70 but python
can handle chaining the expression
together so you don't have to put an and
in there if you're checking against the
same variable so just a nice little
python hack to have
and these we'll call them alifs we know
just by the way we're writing the code
they're mutually exclusive but
this is a good way to do it
and so else we're saying it's got to be
above it because we're assuming that
speed can't be negative i think that's a
valid assumption
for this scenario
and this should handle everything as
long as the cop likes you and then we'll
say else and we'll just go ahead and
copy all this inside code down because
now we're coding for the scenario where
the cop does not like you
which if you're like me is a far more
common scenario
but all we have to do is just knock all
these values down by 10.
and this right here should do it if he
likes me and my speed's under our lower
limit or if he doesn't like me and it's
under 45
no ticket if it's in our defined range
then a small ticket otherwise we're
going way too fast and he's gonna give
us a big ticket
so as always i like to test every
scenario and in this case that would be
that would be checking
our function
and
let's say
let's do the three uh
the three assuming it's false first so
we're to check in all three of our
ranges that we've defined
for
both the true scenario and the false
scenario so what what we should if you
think about what these tests should turn
up
i'll talk about after i type
but we're gonna go ahead and
check
one that should give us no punishment
for both
then we'll check one that should give us
a
small ticket for both and then we'll
check one that should be a large ticket
for both
so i'm gonna go ahead and say if the cop
does like me
i'm driving 53 or if he doesn't like me
i'm driving 44 these should be zeros
and then if i'm in that middle range for
the two
these should be ones and then if i'm
going above my upper limit for either of
them these should be two so i'm assuming
we're gonna see
zero one two zero one two
let's go ahead and run it
okay and zero one two zero one two so we
can see right away
we've correctly coded
for all six potential scenarios so let's
just revisit
the mission if speed is under 45 no
punishment returns zero speeds between
46 and 60 small ticket return a one if
speed is 61 or greater large ticket
return of two if the cop likes you you
get to add a 10 to all those speed
conditions
so
we come on down
cop doesn't like us and we handle all
three situations cop does like us the
values go up
and we get 0 1 2 0 1 2. this is one way
you could code it if you coded another
way
that worked for you that's awesome just
let me know how you change this code up
in the comments um and if you have any
specific questions about what you saw
here feel free to drop a comment asking
me about that and i'll try to get back
to you as soon as possible
otherwise that's problem seven
problem eight
write code returning when your alarm
clock needs to be set for as a string
given two parameters day of week has an
integer where one is sunday and seven is
saturday
and a second parameter whether or not
we're on vacation
which will be true false determine alarm
time
if it is a weekday and not vacation
alarm should be 8 am if it is a weekend
and not vacation oh
this should say and is vacation
alarm should be 9 a.m if it is a weekend
and we are on vacation
a weekday and we are on vacation alarm
should be 10 am if it is a weekend and
we are on vacation the alarm should just
be off
so let's go ahead and create a function
and call it
alarm time
with our two parameters defined as day
and vacation
and let's just start by checking if
vacation a lot of times you'll see me
right away create kind of an if else
loop
um when one of my parameters is a
boolean i like to right away
separate that into an if else because
there's only two scenarios so
you can do all the rest of the code like
work now we're gonna check if it's a
weekday which basically means it's
greater than a one for sunday and it's
less than a seven for saturday
and if that's the case
then we can return
10 a.m right so this is saying we are on
vacation but it's a weekday so we still
have a alarm for 10 a.m but then else
we can return
off
because now we're saying we're on
vacation
and it's a weekend so we don't need an
alarm
but then in the else condition the cool
thing is we can take our exact scenarios
that we just mapped out here because
we've already defined weekday and
weekend and all we have to do is say if
it is a weekday
make it 8 am and if it's a weekend make
it 9 a.m
and so oh it's gonna probably ask you to
put some spaces around the operators you
don't have to do that but i don't like
when python gives me little squiggles
so sometimes i clean it up
it would have run fine without that but
so this really this should handle all
four situations so just like the
previous example we're going to go ahead
and
check our alarm time function in all
four scenarios we'll go ahead and do a
weekday when it's not vacation
i'll copy this couple times
so this we've got a weekday when it's
not vacation we'll go with a weekend
when it's not vacation
we'll do a weekday when we are on
vacation
and we'll do
a weekend when we are on vacation
so based on this weekday not on vacation
we should see 8 a.m weekend not on
vacation we should see 9 a.m
weekday we are on vacation we should see
10 a.m
and weekend and we are on vacation we
should see off so that's all four of the
scenarios and parameters we were given
so let's go ahead and run this
and we get 8 am 9 am 10 am off just like
we predicted
nice
so if you have any questions about
how we did this or you have an
alternative idea that you think um
might be
you know the solution go ahead and drop
a comment let me know what you did and
if you have any questions about how it
worked just uh let me know below
thanks
problem nine
given four parameters one starting
integer two range length three test
value and four mode
write a function determining if the test
value is inside the specified range
defined by starting with parameter 1
of length parameter 2 return true if the
value is in that range unless the mode
parameter for is true
then return false if the test value is
in range
so we're basically
given the starting point and the length
of a range and then a value to test for
whether or not it's inside the range but
then we have a mode where if it's false
we're checking if it's inside the range
but then if it's true we're checking if
it's outside the range so
you want to return true if mode is false
and the test value is in the range or if
mode is true and the test value is
outside of the range
so if you'd like to attempt this before
we dive into the solutions together i
recommend go ahead and hitting pause now
otherwise we're going to take a look at
the solution together
okay so just like our previous functions
we're going to go ahead and start by
creating
a function
our previous examples excuse me we're
going to go ahead and start by defining
a function
that has all of the parameters we were
given in the description so that'll be
the starting point the length the test
value
and the mode
and let's say if it's not
mode so if mode is false this is where
we're checking if it's in the range
we'll use a for loop and say for i
in range
starting from start
and going to start plus length right so
we're
saying the range of values to check
starts at a starting point and goes
until start plus the length
and then we'll say for that range
if the test variable
is equal to i our our tester our counter
variable
then return true
and then otherwise outside of the for
loop we're gonna return
false
but then since we did if not mode now
we're checking if mode is a one and this
is where we take that exact same code
and we're just gonna be
pretty much flipping the outcome so we
say
if it is in range but mode is active
then we're actually going to return a
false when it's in the mode
in a true when it's not and this code
should really handle all scenarios based
on the parameters we were given so
again let's type up
let's type up the various scenarios and
check
we'll check something that's in range
and out of range
let's do 5 5 23 false we'll check
something that's in range and out of
range when mode is active
and when mode is inactive
so let's do 5 5 23.
so we're saying starting point for the
first parameter
and then length for the second parameter
oh
opening windows
starting point length and then our test
value
so we'll do seven
seven
something crazy like 100
8
8
and something that'll be inside for this
one 13. so you can see right here we
have oh i gotta change these to
true
and then
true
so we have the two scenarios where mode
is active inactive
we have something that is not in the
range of 5 to 9 23 so this at first we
should get false
and then we have something that is in
the range of 6 to 11
so we should get true so it should be
false true so far
but then 100 is out of the range of 7 to
13
and so since the mode is flipped we
actually should get true and then 13 is
inside with the mode bit active and so
we should get
false true true false when we run this
so let's go ahead and see if we did it
right
false true true false
nice so this is just one way of writing
this code there are certainly other ways
you don't have to use a for loop um
you could create an array of integers
based on the parameters you're given and
check that your value was inside the
array
there are a number of valid ways to code
this this is just one way
to
uh go about it but that's how i uh used
it and if you wrote this in a different
way and it worked for you that's awesome
and go ahead and let me know about a
cleaner better way of writing it in the
comments below
otherwise i hope you found these useful
these are very close to actual questions
i've had in python interviews before
and i wish you all the best of luck with
your code and your job hunts and feel
free to give me a like and subscribe if
you found any of the content on this
channel useful
or leave a comment letting me know what
you'd like to see more of for next time
as always i appreciate you checking out
the channel and good luck with your code
thanks bye
