Python Practice Problems! Advanced String Functions and Multiple Solution Scenarios - Episode 5

LeMaster Tech
182 followers
1 Video View·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 string processes and sorting substrings inside of strings, as well as scenarios where you may need to construct multiple valid solutions to the same problem!
This is episode 5 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=n3oJHCgy9vE&t=2s
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/download/#section=windows
hello and welcome back to the channel
this will be episode 5 in our practice
problems for python interviews and test
and exam prep series
and uh today we'll be taking a look at
some trickier string operations as well
as a scenario where you might be asked
to solve multiple solutions
of the same problem
so
if you haven't seen any of the previous
videos for this one you don't really
need to these are some slightly more
complex string operations and then just
some scenarios that i've actually had in
software interviews so without any
further ado let's go ahead and get into
it
problem
given a string return a resultant string
where every character inside the string
is doubled
example pizza turns into p p i i z z z z
a a cat turns into c c a a
t
so this is a fairly straightforward one
but if you're not familiar with kind of
the
concept of for loops or indexing inside
a string it can be kind of tricky so
that's the only clue that i'll give and
if you'd like to attempt this one before
we go through it together i recommend go
ahead and pause the video now otherwise
we'll take the look at the solution
together
okay so let's go ahead and get started
on this one and i'm going to start by
defining a function that i will call
double string
and given an input we are going to
make a resultant string that will start
out empty and we will go through and for
i
in
range
input
so we're creating a for loop here that's
going to run once
per um
oh i'm sorry this needs to be length of
input so we're creating a for loop
that's going to run once per character
in the input string so if you think
about the prompt what we're supposed to
be doing is taking every character in a
string and returning it twice so to make
sure it runs the right number of times
you need something that runs once per
character in the input string
and what are we going to do in here well
we're going to take our result string
and add whatever was previously in it to
whatever item is currently in that index
location i
and we're going to do that twice
so all we're doing is basically taking
one character at a time
and adding it twice to a resultant
string
and then all we need to do is once
that's done
we just go ahead and print
the result
and that's
really it so let's go ahead and run this
function
with
the two that we were given as examples
initially
so we can compare them to what's written
up top
let's do double string with pizza
and double string with
cat
and
let's go ahead and run this and see if i
did something goofy or if we nailed it
okay so if you see right here p p i i z
z
z z zz a-a-c-a-a-t-t
if we want to run it with something
other than we
other than what we already had you know
throw a few states in there ohio
new york let's take a look even down to
the space what we should see here yep
you got a two character space now
because that still counts as a character
in
strings so
right away that's the solution to
problem 13 um you know if you did it a
different way or you have any questions
about this solution go ahead and let me
know in the comments otherwise
we'll move right on
so problem 14
given a string return an integer saying
how many times abc appears inside the
string so for example this string hey hi
abc a c a b c high would give you two
back because you've got abc there once
and abc there once so two individual
occurrences and then inside this string
you'd have it once obviously if those
characters don't appear consecutively
inside a string
they won't it you'll get a zero back so
that's all the hints i'll give if you
want to take a crack at this before we
look at it together i recommend pausing
now
otherwise we'll dive into a solution for
this
so i'm going to call this function i'm
going to start just like normal by
defining a function and i'm going to
call this one
check
abc
that's a good name for it
and we'll take an input just like before
and uh in this one we're actually gonna
be returning a integer not a string so
we'll go ahead and make our initial
counter and we'll set equal to zero
and kind of just like the previous
problem we're going to create a for loop
for i
in range
but think about the length of this one
because you're not really checking up to
the end of your string you're checking
up to the end of your string
minus two characters because you're not
if you don't really care if the last
character in a string is in a because
it's the last character in a string
you're not going to check for a b and a
c after you're already past the length
you need so for this length we're
actually going to do the input
minus 2
and that's how many times we want to
check if our character plus the two
characters after it are a b and c and
that'll make sense in this
next line of code hopefully so we're
gonna say if
input at i
plus
input
at
i
no dictionary
input at i plus one
plus the input
there we go at i plus 2.
if all three of those add together are
equal to a b c then we're going to do
increment the count by one add add plus
equals one
need a colon there
and at the end of this we're just going
to print
print
the count
and
that is really
all we should need to do so see what do
you oh it's just because i used a
built-in word
okay so let's go ahead and check
abc here
and for the two inputs i'll actually
start again by using the two
given as the parameters because
if you're told certain scenarios and
you're told what they
should give you that's not a bad place
to start and see if your code's working
and then you can run through some
additional scenarios
so let's go ahead and run this and we'll
see if we
have goofed it up
and we get two and then we get one and
that's pretty cool so let's go ahead and
change this up and say
no
a b c and i'll throw some spaces in
there and then i will say
abc abc
b c a b c a abc so that should probably
give us three
counted kind of quickly
four
a b c
a b
oh yeah no there's four all right i'm
not crazy
cool
um
that's funny actually bca twice i snuck
in abc and didn't even notice so this
obviously this works pretty good this is
an example of how you would sort of
check for a sub string inside of another
string
you can kind of imagine you could set
this code up to where if you were given
any string and you wanted to check if it
was inside of another string
you could set up similar checking code
even if it was a second input
you could set up similar check code to
see if it was equal to your second input
string rather than abc
and you could do a for loop where for
however long your input string was you
could break this out even longer so this
has a lot of applications particularly
in things like checking for duplicate
entries in a data table or checking for
duplicate usernames passwords things
like that
checking for a substring is a pretty
common and useful scenario
so i'm sure there's other ways you could
have written this if you have another
way you prefer that worked for you go
ahead and let me know about in the
comments otherwise that's problem 14
thanks
problem 15 given a string return an
integer saying how many times cool
appears inside the string however if the
string not appears anywhere inside the
string or cool does not appear just
return not cool so basically we're
either returning an integer if cool is
in the string telling you how many times
cool is in the string unless not is also
in the string
or cool doesn't appear at all then
you're returning text saying not cool
so if you'd like to take a crack at this
one before we look at it together i
suggest pausing the video now
otherwise we'll go ahead and check out a
solution for this
so to start i'm going to create a
function just like always and i will
call it cool
or
not and you're going to be given a
string
and the first thing i'm going to do in
the string because it makes sense to me
and again there are so many different
solutions to this i'm just going through
one that works for me
but the first thing that makes sense to
me is i'm going to check and see if not
is in the string because if not is in
the string anywhere it actually doesn't
matter if cool is or isn't in the string
because we're going to return not cool
just because the word not is in there
because the two scenarios are either
cool is not in the string at all which
is return not cool anyways
or cool is in the string but not is also
in there so in either case we return not
cool
so i'm going to create a true false
checker
called not check and we'll have this
boolean variable
and i'm going to check now for i
in range
and for this one not is three characters
so
just like the previous example our
length is actually going to be the
string
minus two
because we are checking
i
and then i plus one and then i plus two
to check for n o and t
and we will do the same if string at i
plus string at
i plus one
plus string
at i
plus two and if that's equal to
not
then we're going to do two things we're
going to make the not
check
equal to true
because we've found not
and then
if string is not
oh actually there's one more condition
we want in here too we want to say and
not check
is false
because we
don't want to keep running this if we've
already found not we don't want to
return
okay we found not multiple times like if
you had a string that was not not not
not cool you wouldn't want to print not
cool four times you just want to print
it once and move on
so we're going to set not
the not checker equal to true and we're
just going to print out
like defined not cool
but then we have to think about the
other scenario so now if uh
if we went through and we didn't find
not then we've got to
actually so we don't want it to be an
else we want it to be at the end of the
for loop
we want to say if
the not check
oh boy having trouble writing an
underscore
so if the not check
is still false after that whole for loop
runs
that means that they went through the
whole string and didn't find not in it
then
this is where we would want to put all
of our cool checking so in here i can
say for let's say j just so it doesn't
get confusing you could reuse i but i
don't want to do that so we'll say j in
range and now we're checking something
that's going to be the length of our
string
minus
um
because now we're checking for cool and
so we're gonna i'm gonna go ahead and
start by copying
this guy because it's actually fairly
similar
so if the string at j plus the string at
j plus one plus the string at j plus two
get rid of this checker
and
we need to add in plus the string
at j plus three
but if all four of those added together
is equal to cool
then we want our count
so that's another variable we should
define initially count
is initially zero
and we want our count to go up by one
every time it finds cool inside of the
loop
and
now what we'll say in here after the for
loop is done we will say
okay
if count
is
if count is equal to zero
then we are going to
print
not cool
and then we will say else
print
and we're just going to print count
and actually these should both be inside
of the if statement so that we don't run
this guy and then run this anyways
so let's see that's kind of a lot uh
kind of a lot that we just threw
in here
and i'm pretty good at making silly
syntax errors so let's go ahead and
check
cooler not against a couple strings
let's do one that just says
cool code man that should give us just a
one
let's do
cool or not and we'll say totally
not
cool man that's probably just gonna tell
us not cool
and let's do one that has to count a few
times we'll say cool cool cool so we
should get one
not cool and then three hopefully let's
go ahead and run this
see how we did
one not cool and three hey not bad for a
first check
um
so
it this is kind of just what makes sense
to me a good way to do it again this
problem has
at least a dozen different ways you
could go about it but the basic check is
you want to make sure the knot condition
is not in there before you do all the
work to check for cool because you
there's really no point in just counting
up counting up how many times cool is in
there and you do all this extra
computing if not was in there the whole
time and you knew your outcome was going
to be not cool
it's like
trying to check out the wiring for a
light switch before you even have
like power available to the building and
it's like there's no way to know your
wiring is correct if the power is
completely off so
that's a little bit of a tricky one
but it's a pretty cool scenario it kind
of builds on the previous example where
you check for a string inside of another
string this one's kind of conditional
checking and deciding what to do based
on a subset so that's a pretty cool
scenario
and hopefully you found it useful if you
have any questions or you think there's
a totally better way to do it just let
me know about it in the comments
otherwise we'll move on
okay problem 16
in the range 1 to 100
if the integer is a multiple of 3 return
rocket
if the integer is a multiple of 5 return
fizz and if the integer is a multiple of
15 return rocket fizz otherwise just
return the integer
so
they're telling you a few things here
they're telling you right away you have
to run this loop a hundred times they're
telling you what to output on multiples
of three
print out rocket on multiples of five
print out fizz multiples of three and
five basically fifteen return rocket
fizz otherwise just print out the number
so
they give you a lot of definition but
there's really quite a few ways you
could go about doing this so if you'd
like to take a crack at it
i recommend pausing the video now
otherwise we'll go ahead and take a look
at a solution together
okay so i'm gonna just start right away
with a for loop because it doesn't
really make sense to create a function
for this one because there's no
parameter game passed in you're sort of
told how it needs to run and it's going
to run the same time every way there's
there's no like given a string or
anything like that so we're just going
to start with a for loop and say for i
in range and it's going to run hundred
times uh you know starting at one going
to a hundred the cool thing about
ranges is it really runs up to that
number of times um but it's
non-inclusive
so i in range 100 is going to run from 1
to 100 or you can think about it
0 to 99
either way
we're going to start then by
incrementing our counter right in the
beginning um
actually don't need to do that in a for
loop silly
uh so what i'm going to do to check is
i'm going to check and see if i
divide by 3 has no remainder
so this is the python function for
remainder when you divide an integer so
you gotta think this i is going to be
um
one two three four five six seven eight
nine any time it is a multiple of three
the remainder is going to be zero so
first condition was if that's true
and
and if i divided by five
does not
have a remainder of zero so this is
saying it is a multiple of three it's
not a multiple of five then what are we
going to do we're going to print out uh
rocket correct yeah
and now we're going to copy this
condition a few more times because in
total there's four things we could
return
and the basic
format is going to be pretty similar for
all of them except the last one will
just be an else
so now let's say if
it's not a multiple of three
and it is a multiple of five then we are
returning
fizz
um
and now you could simplify this to say
if it is uh and let's make these l if's
um it should be mutually exclusive the
way we wrote our conditions but we never
want this to run
and do two of these in the same time so
you could write this as if it is a
multiple of 15 or since we already have
this framework created you can just do
it as it is a multiple of three and a
multiple of five
and in that scenario we're going to
print rocket fizz and otherwise we are
just going to print our counter variable
and this should be
all we need to run let's take a look
there is a chance maybe this is going to
run 0 to 99 so let's
just run it that's the nice thing about
code yeah so it looks right there like
we stopped at 99. so actually the
incrementing of the counter right in the
beginning is what you want to do to make
sure it actually runs
uh
one to a hundred rather than zero to
ninety nine
okay so let's go ahead and just look up
at the top and we'll scroll through a
little bit of it we see our first
text at three and we get rocket which is
great because it's a multiple of three
and then we get uh at five we get fizz
six is rocket rocket fizz threes and
fives and then let's go down to our
first 15 rocket fizz that looks good we
see the numbers in between
let's go down to 30 rocket fizz cool
so uh we can see that code's working
pretty well um this is an actual
question i got asked interviewing for a
python software position and
um
you know the this actually goes with the
next problem here but um this is just
kind of a typical interview question
it's not hard
they give you a lot of parameters and
they just kind of want to understand
your thought process so
that's one way to do it
it worked for me
one thing
about interview questions in particular
and like test and exams where you have a
computer in front of you
go ahead and run it and
see what you get and even if you're on
an interview you know as long as you're
familiar with the basic steps of
troubleshooting and you can talk someone
through your process about how you're
going to go through and catch stuff
usually seeing that you're a problem
solver with an intention on figuring out
what's going wrong is more important to
these people than seeing that you ace
the code on your first try
um so kind of there how we saw it went 0
to 99 and then
quickly fixed it to be 1 to 100 that's a
pretty normal thing to have in an
interview so
that's my solution that's my advice to
you guys let me know what you think in
the comments and we'll move right along
okay problem 17 is the exact same prompt
as problem 16 but now you have to do it
a different way and again this is
actually what i was asked um after
creating code and feeling so triumphant
about having a solution the interviewer
was like okay that was cool um do it
another way you know so using a
different concept
and that's pretty normal if you write
code and you're happy that it works but
then it's not optimized or it doesn't
play nice with someone else's code i
mean typically you don't create software
in a vacuum you're on a team and so it
just
it pays to show you can find multiple
solutions to solve the same problem
so i recommend go ahead and try pausing
it and doing it a different way from how
you did it previously or how we went
through it together previously and take
a crack at it and then we'll take a look
at another solution together
okay so
in my mind what we did previously is we
just did a for loop for every integer
and we used um remainders in division to
check if we were on a certain count
and what i'm going to do this time is
i'm actually going to create a couple
counters
that you can think of like uh
like on an on a um you know like
combination lock once you turn something
over
right like you go zero one two three
four five six seven eight nine but it's
circular and you turn it over and the
next number's nine
so i'm gonna create a couple of counters
we'll do a three counter
and we'll call this other one five
counter
and essentially we're still gonna need a
for loop um
and we can say for
i in range
100
um
or i mean sometimes they'll say even you
know
they don't want to see the same for loop
you could just say
while i
is
less than 100
and you just start by defining i is zero
and then walleye is less than 100 we'll
increment i just like before
we'll
and then we'll start doing some checks
and we'll say if
3
counter
is equal to three and
five counter is not equal to five so
we're kind of creating those same
scenarios as before
but now we're doing it right with
counter variables and so what we want to
do in the situation where we print out
rocket
we also want the three counter to reset
back to one
and we want the five counter to just
increment
okay
so
because we're not using a for loop and
right away you can see this is a lot
more code um you don't have to worry
about like if you're asked to provide an
alternate solution
and it sucks like it's a worse solution
that's you don't have to worry about
that i mean they're trying to see that
you can
solve something multiple ways they're
not trying to
test you and see if there was actually a
better solution
so think about the scenario where
now we're saying that
it's the five counter we want to print
out fizz
let's make it an l if just like before
and now let's consider the scenario
where they're both true
and that's when we print out
rocket fizz
and both counters
get reset to one
and then the other scenario
is going to be
none of that's true and that would be a
scenario where we increment both these
guys and just print out
the
counter
so you print
i
and you increment both counters
and let's check here
rather than spend a bunch of time
reviewing if i've missed something i'm
just gonna look at the solution and see
if we got it
so the end looks pretty good let's go
ahead back up to the start and check
again so at three we get rocket five we
get fizz six we get rocket
15 is rocket fizz let's go 30 rocket
fizz 33 rocket 35 fizz cool
so
again i like the first way we did rocket
fizz um
i liked the for loop it was way less
code you can tell just click in between
the two we did this in
seven lines of code and we did this in
20 i mean this is not optimized um but
you never know like for the application
that you need code written in there are
better and worse solutions and this is a
pretty good
example of you're going to be asked to
solve the same problem two different
ways and a lot of
software guys a lot of tech guys they
get very stubborn in like well i know
how to solve the problem
but a hiring manager or an evaluator
might be trying to see that you can
think creatively of multiple ways of
solving something so
hopefully you found that useful
you know if you solved it a totally
different way if uh you have any
questions about anything you saw here
just let me know in the comments um
otherwise if you found this or any
content on the channel helpful
go ahead and leave a like and a
subscribe i really appreciate that helps
the channel out a lot
and as always good luck good luck with
your code and thanks for watching thanks
bye
