Operators Used in DSA
"Simple code is easier to reason about."
Introduction
Python provides a rich set of operators that make coding interview solutions concise and expressive.
While Python supports many operators, only a subset appears regularly in Data Structures and Algorithms (DSA) problems.
This chapter focuses on the operators you will actually use in coding interviews.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Floating-point Division | a / b |
// |
Floor Division | a // b |
% |
Modulus | a % b |
** |
Exponentiation | a ** b |
Example
Output
Floor Division (//)
Floor division returns the quotient without the fractional part.
Output
Very common in Binary Search.
Modulus (%)
Returns the remainder.
Output
Common interview uses:
- Even/Odd checking
- Circular arrays
- Hashing
- Bucketing
Example
Comparison Operators
| Operator | Meaning |
|---|---|
== |
Equal |
!= |
Not Equal |
< |
Less Than |
<= |
Less Than or Equal |
> |
Greater Than |
>= |
Greater Than or Equal |
Example
Logical Operators
| Operator | Description |
|---|---|
and |
Logical AND |
or |
Logical OR |
not |
Logical NOT |
Example
Assignment Operators
| Operator | Example |
|---|---|
= |
x = 5 |
+= |
x += 1 |
-= |
x -= 1 |
*= |
x *= 2 |
/= |
x /= 2 |
//= |
x //= 2 |
%= |
x %= 2 |
Example
Preferred over
Membership Operators
| Operator | Meaning |
|---|---|
in |
Exists |
not in |
Does Not Exist |
Example
Dictionary example
Set example
Membership testing is heavily used in BFS and DFS.
Identity Operators
| Operator | Meaning |
|---|---|
is |
Same object |
is not |
Different object |
Example
Avoid
Bitwise Operators
| Operator | Description |
|---|---|
& |
AND |
| |
OR |
^ |
XOR |
~ |
NOT |
<< |
Left Shift |
>> |
Right Shift |
Example
Output
Bitwise operators appear in:
- Bit Manipulation
- Masks
- Subset Generation
- XOR problems
They will be covered in detail later.
Operator Precedence
Understanding precedence prevents subtle bugs.
Example
Output
because multiplication happens first.
When in doubt, use parentheses.
Output
Common DSA Examples
Binary Search
Even or Odd
Membership
Counting
DFS
Time Complexity
Most operators execute in O(1) time.
Exceptions include:
- Membership checks on lists → O(n)
- Membership checks on strings → O(n)
- Membership checks on sets and dictionaries → O(1) average case
Common Mistakes
Mistake 1
Using
instead of
when computing array indices.
Mistake 2
Using
instead of
for None.
Mistake 3
Forgetting operator precedence.
Always use parentheses when expressions become complex.
Mistake 4
Assuming
is O(1).
It is O(n).
Best Practices
- Use
//for integer division. - Use
%for parity and cyclic indexing. - Use
+=for counters. - Use
is Nonefor null checks. - Prefer set or dictionary membership over list membership when performance matters.
Key Takeaways
- Arithmetic, comparison, logical, and membership operators appear in almost every coding interview.
- Use
//when calculating indices. - Understand the complexity of membership operations.
- Bitwise operators become important for advanced problems.
- Parentheses improve readability and prevent precedence mistakes.
Related Topics
- Membership Operators
- Bit Manipulation
- Binary Search
- Sets
- Dictionaries
Practice Questions
- Why is
//preferred over/in Binary Search? - What is the difference between
%and//? - Why is
target in numsslower for a list than for a set? - When should you use
isinstead of==? - What does the XOR (
^) operator do?
Summary
Operators are the building blocks of every algorithm.
A strong understanding of Python's operators helps you write cleaner, more efficient solutions and avoid common interview mistakes.
In the next chapter, we'll focus specifically on membership operators, exploring how in and not in behave across different Python data structures and why their performance characteristics matter in interview problems.