Python Practice Problems - ACE a Python Interview! Advanced Logic and Looping Functions - Episode 4

LeMaster Tech
183 followers
7 Video Views·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 advanced logical processing and looping alongside and, if, else, or statements inside functions!
This is episode 4 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=khwiOEdUOm4&list=PLsFyHm8kJsx12xGZkMf8zcT0P8owSOhtN&index=3
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 is going to be part 4 in a
practice problem python series for
interview prep or test or quiz review
this series is assuming you have a basic
fundamental knowledge of python i have
created a tutorial series on this
channel if you're unfamiliar with the
basics you can go ahead and check that
out i'll leave a link otherwise we'll go
ahead and dive into these problems these
are going to cover more
advanced loops and logic scenarios and
are very similar to questions i've been
asked before
on python during interviews
problem 10
we want to build a bridge that is
parameter goal long in meters
we are given a number of one meter small
lengths of steel we are also given a
number of five meter large lengths of
steel
create a function that returns true
only if we can successfully build a
bridge exactly the goal in length
so
you're going to be given parameters of
small
large and goal all as integers and you
only want to return true if you can
build a bridge that is exactly the goal
in length
if you'd like to take a crack at this
before we look at the solution together
i recommend you pause the video now
otherwise we'll go ahead and take a look
at the solution together
okay so to start off for this problem
we're going to go ahead and define a
function that we'll call build
bridge
and
just like we were told we're going to
accept parameters small
large
and goal
and
right away an easy check we can do to
just make sure that we have enough
material
is do if large times five
right so that's how many meters we have
of material
in with large segments plus the small
value
is greater than or equal to
greater than or equal to the goal then
we have at least enough total material
so we know if that's not the case
we can return false because our our
large segments and our small segments
combined are not even enough to get um
to get to the goal
but then we have to start doing some
actual advanced checks because it's not
just simple enough to say well we have
enough material so we can do our goal
because remember
if you have enough material but you end
with a large segment
and you don't have enough small segments
to make up the exact value of the goal
then you actually can't build the bridge
so to start let's just go ahead and
check another easy scenario and say if
the small segments alone are greater
than or equal to the goal
we can return true because that's saying
we have one meter building blocks that
are enough material so even though we
could use some larges in this scenario
we know we have the building blocks
required
so then the second scenario is going to
be a little bit more complicated because
now we actually have to check
that our large times five
is greater than or equal to the goal but
not just the goal it's going to be the
goal
minus the small pieces because we're
saying
if the large is
is times five is just greater than the
goal that doesn't really help us because
the goal could have a remainder it might
be too far off of a multiple of five and
we don't have enough small pieces to
cover up for that
so we add we put an and statement in
here and say the small
is greater than or equal to
the goal
divided by fives remainder and so to to
get a remainder in python that's this uh
percentage sign that's actually the
it's called the modulo or something
crazy like that operator
but in python that gives you the
remainder so we're saying
if the goal
divided by
5
remainder
is less than or equal to the number of
small blocks we have
and
the large number of blocks times five is
greater than the goal minus the small
blocks in that scenario we also would be
returning true so there we're checking
that between large and small together we
have the correct
number of
blocks to build the bridge and so other
than those two scenarios
we actually can return
false
and you should be able to iterate this
you can run this function as many times
as you want to test the scenarios and
this should catch all of them so let's
go ahead and just do a few now
so let's go ahead and print build
bridge
let's say three
one
and six so here's one
where we should be able to build it
because this one big piece is five and
then we can add one of the ones for six
then let's do print
bridge
let's do something with a lot of pieces
53
1
58 and in this scenario we have exactly
the correct amount right we have 53 one
segment pieces
one five segment piece so it's going to
be an exact
and then let's do one that
uses just
because you can also have zeros of
small or larges
so okay that'll be our
should probably
i've done that
my syntax is all goofy
alrighty
let me just grab a few
other scenarios real quick
all right so let's go ahead and run this
and we can actually talk about those
solutions that we get oh
i
build bridges
there we go
so
looking at this
this these should all make sense one
five block plus one one piece of of
steel can make six total one five plus
53 can make 58 you can use nine big ones
to make 45 you don't need any smalls
then we get into a few falses you see
right here with two big blocks and three
single blocks you have 13
total meters of material but there's no
way you can get to nine because you're
either at five plus three which is eight
or you have to go to two big ones and
you're already at 10. so that's why we
got false there
and then again we have 53 singles and
two blocks of
five we actually don't have enough
material overall so we just
don't have enough and then here covers
the final scenario which is you just
have enough small pieces to build the
whole thing you don't even need five
meter lengths
so this is one solution to this problem
again the cool thing about writing code
is there are a dozen ways to solve every
problem and actually a lot of the times
in interviewer in interviews
interviewers would ask you okay your
code works but what's another way you
could solve that code they're not just
interested in seeing that you can write
code checking if you can build bridges
they want to know that you have multiple
attacks to
solve problems so if you coded this in
another way um that's awesome that's
great
it helps to know multiple ways to code
things go ahead and leave a comment
letting me know how you would have done
it or if you did it another way that
worked for you and if you had any
questions about what i did as well go
ahead and ask that in the comments below
otherwise that's the solution to problem
10.
problem 11
we want to add together four integer
values but if any of them are not unique
values they do not count towards the sum
only numbers that appear in the
parameters only once get counted towards
the final sum so you'll be given four
integer parameters
and you want to add them all together
except if there are duplicates they do
not get to get added
if you'd like to attempt this one before
we go through the solution together i
recommend pausing the video now
otherwise we'll go ahead and take a look
at the solution
so to start this one we're going to
define a function called i'll call it
unique sum you can call whatever you
want a b c
and d we're gonna grab four parameters
just four letters and we're gonna start
by setting the sum that we're gonna
output
equal to zero and i'm just going to use
python's not equals to the exclamation
mark equals
and check if
a is not equal to
all of the other values
2c
and d
and if
that is true then the sum
gets added
to itself and a so this is really saying
sum equals sum plus a this is just
python shorthand for adding itself
to adding this value to itself
and then i'm just gonna go ahead and
copy
this code
for
b c and d and start
modifying it
so i
some people i'm sure are going to
come up with cool loops and
and
find ways of passing a parameter through
and doing the same code four times i
mean that's kind of the point of python
i'm just doing what uh works for me and
what i'm pretty confident would be a
quick solution
a big thing i've found in interviews is
python questions from interviewers a lot
of times
are not immediately checking to make
sure you wrote elegant and clean code
you always have time to go back and
iterate and revise code
they want to see you think on your feet
and they want to see you take problems
and
do your best to solve them on the fly so
so right here we check that a is not
equal to b c and d and then we add a b
is not equal to a c and d add b c is not
equal to a b or d at c and d is not
equal to a b or c at d
and then the last piece is you just
return
the sum
and that should do it so if we go ahead
and
let's just let me grab a few scenarios
if we go ahead and print out a few of
these we can look at our solutions so
right here in this first one one two
three and four no duplicates so them all
add together is ten
in the second scenario you can see
they're all duplicates and so we
actually get zero because we don't add
anything
then in the following one you see we've
got a couple of twos that shouldn't get
counted so we're just adding nine and
five for 14
and then we have two ones on the ends
that are duplicates so we just add two
and three for five
and then another one with no duplicates
and we get 22.
again there are
ways to cut down on the total amount of
code shown in here
but this method works and if you have
another method that works that's awesome
go ahead and tell me about it below
or ask any questions you have in the
comments otherwise that's the solution
to problem 11.
problem 12
given three integers as parameters check
to see if they are all spaced out return
true if they are all at least three or
more away from each other by value
and also return true if two or more
numbers are identical but still spaced
out by three or more from other values
so you're given three integers as
parameters duplicates can still count as
spaced out because even though those two
numbers aren't more than three away from
each other they're the exact same number
and so for this scenario that's going to
count
so if you'd like to attempt the problem
before we go through it together i
recommend you pause the video now
otherwise we'll take a look at the
solution
okay so we're going to go ahead and
start by just defining a function that
i'll call
distance checker
and let's go ahead
and pass in three integer parameters and
let's do the first easiest check which
is if we don't have any duplicates
let's check the absolute value which is
abs in python
of a minus b and let's check if that
difference is greater than or equal to
three
and we can do this a couple times
and we'll do a minus c
and then we'll do
b and c's difference
and it that this is the easiest scenario
it's just none of them are duplicates so
you can return true
if they all
are at least spaced out by three
and then the next scenario would be
if a
is equal to b
and the difference between
you can say a or b minus c
um
doesn't matter
but as long as that spacing is greater
than or equal to three
then we're still spaced out right so
we're saying okay two of them are
identical
and then the third one is more than
three away from it
and so we need a few ore conditions in
here because the other scenario could be
c is equal to b
but then we would need
still the difference between a and c to
be greater than three
or
the last scenario is a is equal to c
and then the difference is we would need
the difference between b and c to be
greater than three
and we could return true for that
scenario and the last scenario is an
easy one is just a
is equal to b
and
a
is equal to c and so if they're all the
same
even though it seems silly because
they're not really spaced out they're
just the same number we're still going
to return true
and then any other scenario we return
false
so to test this i'll go ahead and pull
in some
distance checking
scenarios
and let's add one in here that i just
thought of where they're all the same
three three three
okay so i'll go ahead and run this and
we'll take a look at the solutions
together
if
else return oh
that should say return false not just
false
okay so let's take a look at what we've
got here one one and seven so we have
two duplicates and then a number that's
more than three away so we get true
then one three and five those are all
within two of each other so we get false
1 3 and 19 even though 19 is way out
there one and three are too close so we
get false
1 7 and 21 just spaced out nicely and we
get true and then 3 3 and 3 they're all
the same and we get true
so this is how i chose to do this if you
went about it another way that's cool go
ahead and drop your solution in the
comments if you had any questions about
this problem or any of the problems you
saw today don't hesitate to ask
otherwise thanks for checking out the
channel if you found it useful
go ahead and leave a like and subscribe
and if you want to see something in
particular just let me know send me a
message and i'll try to get a video out
on that so
as always thanks for checking out the
channel and good luck with your code
thanks bye
