#OneYearCodingPlan - Big O, Arrays and Strings - by Anjali Viramgama - The Startup - Medium

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

This is your last free member-only story this month.

Sign up for Medium and get an extra one

#OneYearCodingPlan: Big O, Arrays and


Strings
Anjali Viramgama
Jul 19 · 6 min read

#OneYearCodingPlan : Month 1
In this article, we’ll discuss time & space complexity, arrays, and strings, for coding
interviews. Before we begin, I’d like to discuss the structure that I’ll try to follow in my
blogs.

Format:
1. Topic overview and Learning Timeline to be followed.

2. Resources to understand the topic and practice questions.

3. Interview tips.

Disclaimer — One month is the worst possible deadline you can have for these topics,
you have to wrap this up in a month(remember sticking to the plan?), and you can
(and should) try to finish early, by spending more time per day, especially on
weekends, and jumping to the next topic, especially if you have a coding background
and feel confident about these topics. The OneYearPlan can be scrunched down to a
12-week plan if you have prior experience or tighter deadlines. However, don’t try to
rush if you have time, you should preferably do projects, research, or gain experience
on the side if you have extra time.
Source — websystemer.no

Time and Space Complexity


Estimated Time: 3 days.

It is extremely important to understand time and space complexities because they help
you judge how good your solution is, and brainstorm about if and how you can make it
better.

Understanding the Big O:


Big O can be best understood by solving examples/jumping into problems after a little
bit of reading.

GeeksForGeeks

HackerEarth

Interview Cake

Why is the constant dropped off? An important concept, well explained here.

Practice Questions:
Read these examples first. (GeeksForGeeks again)

Solve all interview bit questions.

Interview cake questions.

After these questions, you should be able to know the basics. This is a trivial topic, but I
am assuming you have basic knowledge of recursion, etc. If not, you can spend some
extra time in understanding it. After this, whenever you solve any question in the
future, you should be sure what its space and time complexity is, and you’re good to go.
Here’s a question for you after you think you are ready:

Find the time complexity of the c++ function, quuz() , which is invoked by the

function corge() , which takes in an array A , along with its size n .

// swap two numbers. a and b are passed by reference


void swap(int& a, int& b) {
int tmp = a;
a = b;
b = tmp;
}

// find a partitioning point within the array


int bleh(int A[], int p, int q) {
int r = A[q];
int i = p - 1;
for (int j = p; j < q; j++) {
if (A[j] < r) {
i++;
swap(A[i], A[j]);
}
}
if (A[q] < A[i + 1]) {
swap(A[i + 1], A[q]);
}
return i + 1;
}

int quuz(int A[], int x, int y) {


if (x < y) {
int z = bleh(A, x, y);
quuz(A, x, z-1);
if (z < y) {
quuz(A, z + 1, y);
}
}
}

int corge(int A[], int n) {


quuz(A, 0, n-1);
}

In python:

# swap two numbers in a lst


def swap(lst, a, b):
lst[a], lst[b] = lst[b], lst[a]
# find a partitioning point within the array
def bleh(A, p, q):
r = A[q]
i = p - 1
for j in range(p, q):
if (A[j] < r):
i += 1
swap(A, i, j)
if (A[q] < A[i + 1]):
swap(A, i+1, q)
return i + 1

def quuz(A, x, y):


if (x < y):
z = bleh(A, x, y)
quuz(A, x, z-1)
if (z < y):
quuz(A, z+1, y)

def corge(A, n):


quuz(A, 0, n-1)

Interview Tips:
The coolest part about Big O is, most companies ask the same question about it:
“What is the time and space complexity of the solution you just coded?” & “Can you
make it better?”

Whenever you are asked a question by an interviewer, always start talking out loud
about your thoughts/solutions. Start by stating out the brute force solution, and then
move on to better solutions, and always include the time and space complexity of
the solution while discussing it, that leaves a good impression. Then start coding the
best solution you can think of.

Another reason why this topic is important is when you participate in coding contests,
you might get a “terminated due to timeout” error, which is mostly caused when they
are expecting a better solution from you in terms of the Big O.

Companies like Google, Bloomberg, etc. pushed me to solve the same question in
various different ways, with different complexities. In my first round with Google,
when I solved the interview question and we had time left, the interviewer pushed me
to think of different ways to solve the same question/use different data structures,
though the solution I had given was optimal. Recruiters generally are more interested
in the way you approach the problem and the different solutions you can come up with
than you just solving the question presented to you.
Arrays and Strings:
Learning the syntax, methods, and reading — 1 day

Solving easy Array problems — 5 days

Solving easy string problems— 5 days

Solving medium Array problems — 5 days

Solving medium string problems — 5 days

Medium/Hard problem practice on both topics — 6 days

If easy problems seem too easy for you, always switch to the next step, and use all the
extra days to solve more hard problems. Aim to spend at least 3–5 hours a day,
regardless of how busy you might be.

Resources for learning :

GeeksForGeeks

TutorialsPoint

Small Project idea (amateur)-


Build a MyArrayList structure on top of an array that behaves like an ArrayList. It
should have an initial capacity of n, which will be inputted during object creation. It
should automatically expand to double it’s the size when it is more than 75% filled and
should support the following methods:

AddtoFront(element), AddtoBack(element), Addatposition(index, element)

Deleteposition(index)-Delete element at position index, clear()-remove all elements

length()-Size of the ArrayList and not the underlying array

get(index)-Return the element at position index

join(MyArrayList b)- Add all elements of ArrayList b behind the current elements in the
current object, and return the entire structure.

Practice Questions:
String Manipulation

Arrays

More Arrays

These resources are just suggested in case you don’t know where to start. If you are a
user of any other platforms (CodeChef/ Code Forces etc.) use them. I’ve never
personally used them, so I don’t want to preach what I don’t practice, but they are
awesome platforms from what I’ve heard as well. What’s important is that you solve a
vast variety of questions, regardless of the source.

Whenever you think you are ready, you should try these questions (you are not allowed
to look up their solutions, if you have issues solving these, try practicing other
problems more.) Also, do these on a whiteboard. These are not necessarily hard
questions, the catch is to be able to do them on a whiteboard.

These 5 questions for arrays, and these 5 for strings.

You have probably heard of balanced brackets problem, Given a string which
consists of only brackets “{“, “}”, “(“, “)”, “[“, “]”, write a program to examine
whether the brackets are balanced.

Example:

Input: exp = “[()]{}{[()()]()}”


Output: Balanced

Input: exp = “[(])”


Output: Not Balanced

Solve the above question with string manipulation( not a stack, use only strings).

Lastly, build a function that randomly selects and returns an element from an array
with a probability proportional to its value. For example, in [1,2,3], the probability
of getting 3 should be 1/2, the probability of 2 should be 1/3, and 1 should be 1/6.

Interview Tips:
Arrays and Strings are vastly underestimated, but most interviews I have given ask
tricky questions from this topic especially in their first round. Arrays and Strings will
always be a subpart of the complex problems you’ll solve later, and having a solid base
here is necessary for success in the upcoming topics.

Feel free to reach out to me for queries, constructive criticism is always welcome! I had
a lot of people ask me to provide resources, so I tried to do that here. If you have any
other suggestions, ping me on my social media!

Thanks,

Anjali Viramgama.

Software Developer Intern at Facebook

LinkedIn | Instagram

Programming Technology Coding Interview Software Development

About Help Legal

Get the Medium app

You might also like