Python Practice Interview Problems - ACE a Python Interview! Strings, Arrays, and Loops - Episode 2

LeMaster Tech
183 followers
2 Video Views·Apr 23, 2025
Prepare for a technical interview for a Python-related Job with these sample code problems! This episode covers arrays, strings, indexing and using for loops to investigate paramater conditions inside functions!
This is episode 2 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=nyrbv...
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 episode 2 and our
python practice problems for python
interviews
and really general python skills i mean
for beginning coders who want to develop
talent you don't have to have an
upcoming interview with python for this
to be useful but this will be episode
two we covered some basic
four conditions and a lot of boolean
checks in episode one so if that's
something that interests you go ahead
and check that out we're not going to be
covering as much of the basic syntax of
python we are going to assume you have a
little familiarity with the different
variables so if you need a tutorial uh
course there is one on this channel i'll
go ahead and leave a link to that but
without further ado let's get into the
practice problems problem four
provided a positive integer and a string
variable output a string that is copied
as many times as the integer specifies
so you're going to be given a string and
an integer and you're supposed to output
a copy of that string as many times as
the integer calls for
so i suggest pausing the video now if
you'd like to attempt it before we
discuss the solution together
okay let's go ahead and take a look at
the solution for this problem
so we'll call this
we're going to create a function and
we're going to call it duplicate string
and the two variables are going to be
we'll call it
just str
yeah we'll say string be descriptive
and then integer we will just call it
integer no one has to worry wonder about
anything
and we will make our output just a blank
string variable to start we'll call it
result
and then we will say
for
i that's a counter variable
in the range
and the range will just go up to the
integer that we were given
and as many times so this is saying
every time
as many times as the integer gives us
run the code inside this for loop
and inside the for loop we're going to
say the result
that variable we defined before it's
just blank on the first iteration
is equal to itself
plus our string variable
and so then
we go ahead
and return
the result
and this should do it so to test this
we're going to go ahead and run
we're gonna print it out so that we can
see it
but we're gonna run duplicate string
and let's do string
test
and let's do that twice and then let's
do one other one
and this will say
code is fun
and we will do that four times because
code is
that fun
so here you see we get test twice and
then we get code is fun code is fun code
is fun code is fun i'll work in no play
and right there that's the solution to
problem number four uh if you have any
questions or any um inquiries about
variations or if other ways of writing
this is valid feel free to drop a
comment
um or ask any questions in the comments
below and i'll be happy to get back to
you on that so that's solution four
problem five
given any string that is not blank
return the string incrementally added
together one additional character at a
time see example
so given pizza as a string return p
p i
p i z
p i z z p i z z a so we basically take
the length of the string
and do one character at a time adding
one character each iteration until we
get a string that looks like it being
added together slowly
so if you'd like a second to attempt
this on your own i recommend trying that
now
go ahead and pause the video otherwise
we're going to take a look at the
solution
okay so we'll we'll create a function
just like we always do and we will call
it string
separated
and all this needs to receive is our
actual string variable
and to start we're going to create a
blank string just like we did in the
previous example
and this time we're going to use that
same counter variable for i n but we
haven't been given an integer yet the
range for our for loop is actually going
to be the length of our string variable
so again we're assuming that you know
some basic pi python syntax for these
practice problems a for loop where you
define the range which is the number of
times it iterates
and then this len function is returning
is actually pulling the number of
characters inside of your string
variable and so the number of times we
want this to run
is the length of the string
so
each time that it runs we want it to add
to the previous string that we've
already created
as well as our string variable for
however many characters we've gotten up
to with our counter so obviously the
first time this runs it's technically
zero but we want the first character so
it's gonna be i plus
one and then we're gonna
return the result after our for loop has
completed
so let's go ahead and
print
this
and we'll do it
well we'll say pizza since that's the
one we already uh
have up above and we should be able to
make sure it
runs just like expected p p i p i z p i
z z p i z z a
let's do something wacky like uh
michigan that's wacky
i don't know
it's not that wacky you can see right
away how long this gets i mean but you
can clearly see just m m i m i c m i c h
m i c h i
so
the code is clearly working um
one thing i'll touch on real quick
you've not i'm kind of assuming you know
how you can address indexes within
strings for this example this
colon is how you define a range of
indices and if you don't put a value
before the colon you're saying start
from the beginning so what you could
have done is you could have gone from
i to i plus one and what you'll see is
there you're actually just grabbing one
character at a time you've created a
range of characters and every time it
increments it's just incrementing to the
next character so this colon
is how you define a range of strings to
include in your
in your
result but
the the way indices work is it counts
the starting point and it doesn't count
the end point
that's why on the first iteration it
only gives us the m it doesn't give us
mi because it's finishing at one it's
not finishing at two and counting one it
finishes at one and doesn't count one
so if you have any questions about that
feel free to drop a comment otherwise
that's the solution to problem five
problem six
given an array of integers return true
if the sequence contains three two one
in order at any point
so this will be fun this is our first
exposure to integers in this code series
but it uses some of the indexing that we
used for our previous string example so
that's the last hint i'll give i
recommend pausing the video now if you'd
like to take an attempt at the solution
on your own
otherwise we'll go through it together
i'll just shift that
so just like always we're going to
define
a
code of course we're going to define a
function and we'll call this array321
and we'll be given
well it tells us we'll be given integers
and so that's the title of the array
we're passing in
and then we're going to use a for loop
again just like before
um but this time we've we've seen range
with a defined number of iterations and
we've seen range where we check the
length of a string this time we're
actually going to check the length of
our array of integers
but because we're checking a series of
three we're not just checking a value
we're checking the value and the two
after it it doesn't make sense to check
the entire array because we know when we
get to the end of the array there's no
point in checking anymore because we're
assuming that if the last two values of
the array are 3 2 or 3 and then the
array starts with 1 or 2 1 that does not
count it has to be in order
that's just the assumption we're making
to code for this problem but so the
length we're actually checking is the
length of the array minus 2 because as
we get to the end of the array we don't
want check anymore
and what we're checking is actually if
the
integers
at i
are equal to one
and
oh you gotta spell it right
and the integers at i plus one
are equal to two
and that integers at i
plus
two
are equal to three i'm sorry i've got
this backwards three two
and then one
jurors
and if those are all true
then we
return
true and that is the only scenario where
we want to return true so else
return
false
if integers
it's got to be capitalized and then
let's make sure i didn't do anything
silly here
and that should do it so we'll go ahead
and test this real quick by
oh boy i've lost my mouse
let's go ahead and
print
array
three two one
and let's give it
an array of one one
three two one so there's one that we
would expect now let's throw four five
on there just to be fun there's one we
would expect to return true
and let's give it a second one where
instead of three two one that is three
two one three
four five shake it up a little bit
so i'm expecting true because we have
three two one and then false i do this
kind of quick we'll see if i messed it
up
so let's take a look what have we done
if integers i
no
three
oh i see we're actually returning false
every iteration of the loop this
actually needs to be
outside of the
for loop
there we go
so that was my mistake we put the return
false inside of the for loop and even if
this is true once
then what it's actually returning is
just the last one it's going to execute
so for both of those since it didn't
finish untrue it gave us false the
return false needs to be outside of the
for loop
that way we only get a true in the
situation where it's true
so
that's
that's a sample problem using arrays and
and addressing a range of index values
if you have any questions on those
or would like to see more sample
problems on anything specific please
just drop a comment or leave a like and
go ahead and subscribe to the channel
let me know there's some interest in the
channel and that really helps me with
putting out these videos so feel free to
get in touch if you'd like to see
something specific otherwise i
appreciate you checking out the channel
and good luck with your code thanks bye
you
