FastPrepCount Smaller Prior Elements
Problems/Count Smaller Prior Elements

Count Smaller Prior Elements

Tata Consultancy Services logoTata Consultancy Services● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Tata Consultancy Services online assessment. The idea of counting over an ordered array is kept. We chose a result for each position and a strict comparison. Our version is about 82% the same as the reported question.

Given an integer array nums, return an array answer where answer[i] is the number of indices j < i for which nums[j] < nums[i].

Equal earlier values are not smaller and must not be counted.

Function

countSmallerBefore(nums: int[]) → int[]

Examples

Example 1

nums = [5,2,6,1,3]return = [0,0,2,0,2]

The counts are based only on earlier positions: 0, 0, 2, 0, and 2.

Example 2

nums = [2,2,2]return = [0,0,0]

Equal earlier values never count as smaller.

Constraints

  • 1 <= nums.length <= 200000
  • -10^9 <= nums[i] <= 10^9

More Tata Consultancy Services Online Assessment questions

  • EV Charging Cost OptimizationOA · Seen Sep 2026
  • Maximum Gap After SortingOA · Seen Sep 2026
  • Count Array InversionsOA · Seen Aug 2026
  • Array Rank TransformOA · Seen Jul 2026
  • Gym Fees CalculatorOA · Seen Mar 2026

Similar questions at other companies

  • Photo AlbumHonda R&D · OA
  • Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
  • Minimum Bridge TurnbacksBNSF Railway · OABNSF Railway interview questions
  • Horizontal Pod AutoscalerSnowflake · OASnowflake interview questions
  • Horizontal Pod AutoscalerRubrik · OARubrik interview questions
  • Adjust Prices 🍱DoorDash · OADoorDash interview questions
  • Photo AlbumFactSet · OAFactSet interview questions
  • Maximum Profit from an Increasing Price TripletOdoo · OAOdoo interview questions
  • Count Decreasing TripletsAirbnb · OAAirbnb interview questions
  • Maximize Shipment Order by Sorting One WindowAmazon · OA
All Tata Consultancy Services interview questionsSee Tata Consultancy Services hiring insights
Saved
class Solution {
    public int[] countSmallerBefore(int[] nums) {
        // Write your code here.
    }
}
Checking account…