cswork / libraryOpen practice workspace

Coding questions · Ansys

Implement Prefix Search and Node Removal

This question evaluates proficiency in string data structures and manipulation (prefix-search concepts including case-insensitive matching and regex-based normalization) as well as linked list pointer manipulation and in-place node removal.

This interview included two coding tasks:

  1. Implement a prefix-search data structure for strings with two operations: insert(word) and count_with_prefix(prefix). A follow-up asks how to make matching case-insensitive and how to normalize inputs by removing trailing digits with a regular expression before storing or querying.
  2. Given the head of a singly linked list and a target value x, remove every node whose value equals x and return the new head.

Prefix Search Trie

Process insert and count operations on a trie, with optional lowercase/trailing-digit normalization.

Examples

Example 1

Input: ([[ 'insert','apple'],['insert','app'],['count','app'],['count','appl']], False)
Output: [2, 1]

Explanation: Prefix counts after inserts.

Example 2

Input: ([[ 'insert','Test123'],['count','test']], True)
Output: [1]

Explanation: Normalization lowercases and removes trailing digits.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.

Remove Target Nodes From Linked List

Linked lists are represented as value arrays. Return the values after removing every node equal to x.

Examples

Example 1

Input: ([1,2,6,3,6], 6)
Output: [1, 2, 3]

Explanation: Remove all target values.

Example 2

Input: ([], 1)
Output: []

Explanation: Empty list unchanged.

Hints

  1. Clarify edge cases before coding.
  2. Keep the return value deterministic.