Skip to main content
Back to Topics

Counting Problems (201)

Problems tagged with Counting

About Counting

Counting is a fundamental technique in data structures and algorithms that focuses on determining how many times something occurs. Instead of storing every element or combination, counting methods track frequencies, occurrences, or valid configurations efficiently. This approach often reduces time complexity and simplifies problems that would otherwise require heavy iteration or brute force.

In coding interviews, counting techniques appear in many forms—frequency tracking, counting valid pairs, counting subarrays, or counting combinational possibilities. Interviewers frequently expect candidates to recognize when a problem can be solved by maintaining counts rather than building complex structures. Many popular interview questions rely on counting ideas combined with tools like Hash Table frequency maps, prefix accumulations with Prefix Sum, or mathematical reasoning from Combinatorics.

Common counting patterns include:

  • Frequency counting using maps or arrays to track occurrences of elements.
  • Pair or triplet counting where the goal is to count valid combinations rather than list them.
  • Subarray counting using cumulative counts or prefix sums.
  • Bit-level counting using techniques from Bit Manipulation to count set bits or binary patterns.
  • Window-based counting where constraints are tracked dynamically with techniques like Sliding Window.

You should consider a counting approach when the problem asks "how many" rather than "which ones." Problems that involve duplicates, frequency comparisons, or combinational possibilities often become significantly easier when converted into counting problems.

Practicing counting problems helps you develop pattern recognition and efficient thinking—two skills that are critical for technical interviews. FleetCode provides 190 Counting problems ranging from beginner-friendly frequency tasks to advanced combinatorial challenges. By solving these problems, you'll learn how to transform brute-force enumeration into optimized counting strategies that scale well for large inputs.

Prerequisites

1
Array

Most counting problems start with arrays where you track element frequencies, pairs, or subarrays. Understanding array traversal and indexing makes it easier to implement counting strategies efficiently.

2
Hash Table

Hash tables are commonly used to store frequency counts of elements or track occurrences in O(1) average time. Many counting interview problems rely on hash maps to maintain dynamic counts.

3
Prefix Sum

Prefix sums help convert repeated counting of subarrays or ranges into constant-time queries. This technique is essential for problems that count subarrays with specific properties.

4
Combinatorics

Some counting problems require mathematical formulas such as combinations, permutations, or counting arrangements. Combinatorics helps compute counts without enumerating all possibilities.

5
Bit Manipulation

Bit manipulation techniques are useful for counting set bits, bit patterns, and subsets efficiently, which appears in many advanced counting interview questions.

Page 2 of 2

Practice by Difficulty

Start Easy, progress to Hard.

Related Topics

Frequently appear alongside Counting.

FAQ

Common questions about Counting.

What are Counting problems in DSA?

Counting problems in DSA focus on determining how many elements, pairs, subarrays, or configurations satisfy a given condition. Instead of listing all possibilities, efficient algorithms track frequencies or use mathematical reasoning to compute counts quickly.

Is Counting the same as Counting Sort?

No. Counting is a broader problem-solving technique used to compute frequencies or totals, while Counting Sort is a specific sorting algorithm that uses counts of elements to place them in sorted order.

What is the best way to learn Counting in DSA?

Start with simple frequency counting using arrays or hash maps, then move to pair counting and subarray counting problems. After that, practice problems involving prefix sums, combinatorics, and bit counting to handle more advanced scenarios.

Are Counting problems common in FAANG interviews?

Yes. Counting techniques frequently appear in FAANG-style problems, especially those involving subarrays, duplicates, frequency analysis, and combinatorial counting. Many medium-level interview questions rely on counting with hash maps or prefix sums.

What are common Counting patterns in coding interviews?

Common patterns include frequency maps, counting pairs or triplets, counting subarrays with prefix sums, combinational counting using formulas, and bit counting techniques for binary representations.

How many Counting problems should I solve for coding interviews?

Most candidates become comfortable with counting patterns after solving around 30–50 problems. To reach strong interview readiness, practicing 80–120 problems covering frequency maps, pair counting, and combinatorics is recommended.