Python1: Data Structures: Tuples & Coding Interview Questions
Introducing a new data structure (not data type) called Tuples. We'll talk about some of the pros of working with Tuples. Make sure you completed the Coding section first before reading this.
We are now splitting the coding section into Python, and R programming. Since this section doesn’t have any R content, I highly suggest you bring in your inner autism, you will need it.
Table of Contents:
Data Structures 101
Tuples
Coding Questions
Finishing Remarks
1 - Data Structures 101
A Data Structure models a collection of data, such as list of numbers, a row in a spreadsheet, or a record in a database. Modeling the data that your program interacts with using the right data structure is often the key to writing simple and solid code. Python has 3 inbuilt data structures: tuples, lists, and dictionaries. We will be starting off with tuples first.
Data structures are important because typically in most Python DS roles, at some point you will be asked what you know about data structures and which kinds you have worked with.
2 - Tuples
Tuples are basically a finite ordered sequence of values. Tuples are ordered because their elements are in a specific ordered fashion. We can create tuples using the () function. Here is a simple tuple, with elements: 1, 2, 3 in that specific order.
We can use the type() function to tell us what type of data we are working with. We can also create an empty tuple by just using (), and we can create a tuple with 1 element by using () and a comma (,). Observe:
Tuple & String Similarities
Tuples and strings are very similar:
Both of them have a length
Both of them are perfectly fine when it comes to working with indexing & slicing
Both Tuples & Strings are immutable
Where this is used
Tuples are immutable, meaning we can’t change a specific value in there, once it’s created, similar to strings. This makes Tuples great for when we are dealing with read-only data.
Since Tuples are basically meant for read only data, if the goal is to just run a for loop and try to iterate through the data we have, we are better off running the iteration process through the tuples, since they are faster at it than lists are.
Typically, if we are doing some sort of a find, or a loop in production code, or some sort of protected read only data, we will want to use Tuples as much as possible.
Common Tuple Functions
Length
Tuples, like strings have a length. Like with strings, we can use the len() function to tell us how many elements are in a tuple.
Packing & Unpacking
Tuples let us pack multiples values into 1 variable, basically assigning a tuple to 1 variable. This is called packing. Then we can separate the multiple values from the above created tuple into many different variables. This is called unpacking. In this example, we will pack the strings: ‘Banks’ ‘Are’ ‘Zeros’ into 1 variable. Then we will unpack them into 3 variables.
in
There exists a function in Python called in. In is used to check if a value exists inside something else. We can use the in function to check if a value exists inside a tuple. Here is an example of checking if the number 5 exists inside this tuple.
tuple constructor
If we wanted to create a tuple, but we wanted to make every character in a string it’s own separate element in the tuple, we can use the tuple constructor function. Here is an example:
Tuple concatenation
Similarly with string concatenation, you can even concatenate tuples together using the + sign.
Deleting tuples
Remember, since tuples are immutable, the only real way to change a specific value in there is to do the exact same thing we did for strings. Sometimes however, instead of going through that headache, it is much easier to just simply delete the tuple, that way, once you have everything ready, you can just recreate it again. Here’s how you can delete a tuple once you’ve made it.
min/max/sum
Like with an excel function, we can get the min/max/sum of all the elements inside the tuple by using the min(),max(), and sum() functions.
3 - Coding Questions
Here are some questions you will be asked during your coding interviews. Pay close attention to what they are saying when you are having a conversation with them. If they mention the phrases ‘knowledge of data structures’, or ‘aliases and memory’. This usually means they will get you to do some stuff on basic in-built Python data structures. Usually this means some questions on tuples, lists, and dictionaries.
These questions are not super difficult, they’ll typically ask you how to do these in a verbal manner, not so much of a drawing on the board, or take home.
Reverse the Tuple
Given a tuple, how would you reverse it? The answer is we can use slicing, but just reverse the order in it.
Find and Locate this specific element on the tuple
Given a tuple, where would we locate a specific element? The question is asking to locate an element, not tell if it exists, so we use the .index() function.
The number one is located in the 0th position which means first element. Remember, Python starts it’s indexes at 0.
Swap 2 tuples
Given 2 tuples, without creating any variables, or deleting anything, how would we swap them? We can achieve this by doing a quick unpacking.
Append 1 tuple with another
How would you append 1 tuple with another? They will most likely try to say append to try to trip you up, but remember, append is just another way of saying concatenate. In other words, we can just concatenate the tuples.
Sort a nested tuple by the second item
You have a nested tuple, how would you sort the big tuple, but by looking at the 2nd element in the smaller sub tuples. To do this, we rely on the key, and lambda function.
Again, all of the above were quick rapid fire type of questions, that they could ask you during a verbal technical interview (yes, those exist).
4 - Finishing Remarks
Tuples are nice because they can let us keep our read only data safe. Also, if we wanted to run some sort of an iteration process on them, they are nice for production code since they are quite fast. However, realistically speaking, in your DS role, you will primarily be working with pandas.
I highly recommend going over this post a few times, as this a popular interview topic.
Pandas run almost entirely off of something called arrays, arrays are basically just a list inside a list. So, on the next Python coding section, we’ll go super heavy on lists. We will also speak about things like nested vs shallow copies.
Click here to continue to Python 2: Lists.