AI Hints
Home›DSA in Python Tutorial›Interview Questions
DSA in Python · Lesson 127 of 132

Interview Questions: Part 1

The questions they ask.

⏱ 2 min read▶ Video at 2:30:55🎓 Free
Watch this lesson in the DSA in Python courseWatch on YouTube ↗Full DSA in Python course

Coding interviews often start with questions about the basics, before any code. Short, clear answers show that you really understand the tools. Let's go through the ones that come up most.

Interview Questions: Part 1
From the video: Interview Questions: Part 1
  • Interviews start with basics
  • Short, clear answers
  • The most common questions

List or dictionary?

What's the difference between a list and a dictionary? A list keeps items in order, by position, and finding a value means scanning, which is O of N. A dictionary stores key value pairs in a hash table, so looking up a key is O of one on average. Use a list for order, and a dictionary for fast lookup by key.

keyvalue
Listordered by position
Search a listO(n)
Dictionarykey to value
Look up a keyO(1) average

Stack or queue?

What's the difference between a stack and a queue? A stack is last in, first out, like a pile of plates. A queue is first in, first out, like a line at a shop. Stacks power undo, and depth first search. Queues power breadth first search, and task scheduling. In Python, use a list for a stack, and a deque for a queue.

keyvalue
Stacklast in, first out
Queuefirst in, first out
Stack usesundo, DFS, brackets
Queue usesBFS, scheduling

Array or linked list?

When is a linked list better than an array? A linked list can insert or remove at the front in O of one, while an array has to shift everything. But an array can jump to any position in O of one, and a linked list has to walk there. Arrays also use the computer's memory cache better, so in practice, arrays usually win.

keyvalue
Array indexO(1)
List indexO(n), walk there
Insert at frontarray O(n), list O(1)
Usuallyarrays win

Summary

Know the trade offs. Lists keep order, dictionaries find keys fast, stacks are last in first out, and queues are first in first out. Next, more interview questions.

Key takeaways
  • List vs dict
  • Stack vs queue
  • Array vs linked list
Next up: Interview Questions: Part 2 — trees, sorting and more