Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Characters and Find First

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Count Characters and Find First. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

F5 BIG-IP diagnostics can contain compact log messages that need lightweight character analysis. Given a message and a target character, count every character in the message and return the target's first occurrence.

Implement a function that treats uppercase and lowercase characters as different characters and preserves every character, including spaces and punctuation.

Formal Specification

The input consists of:

  1. s, a string containing zero or more characters.
  2. target, a string containing exactly one character.

Return a dictionary with:

  • counts: a dictionary mapping each character in s to its number of occurrences.
  • first_index: the zero-based index of the first occurrence of target, or -1 if target does not appear.

Constraints

  • 0 <= len(s) <= 100,000
  • target contains exactly one character
  • Character matching is case-sensitive
  • The input may contain letters, digits, spaces, and punctuation

Function Signature

def analyze_string(s, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output