Samsung DSA Question

samsung logo
samsung
June 7, 20256 reads

Summary

I recently took a coding assessment at Samsung and want to share a unique DSA problem involving greedy, two-pointer, and sorting techniques.

Full Experience

I recently took a coding assessment where one of the problems was based on a very interesting twist on greedy + two-pointer + sorting techniques — and I wanted to share the question for the community.


You're on a haunted straight road of length L with N ghosts positioned along it.

Each ghost is described by:

A position A[i] on the road (0 ≤ A[i] ≤ L)

A stamina level B[i]

You are allowed to trap exactly one ghost, and this ghost becomes your trap ghost. Starting from this position, you can move only to the right, hunting more ghosts under the following rules:

Rules for hunting: You may only hunt ghosts whose stamina is greater than or equal to the trap ghost's stamina.

Hunting a ghost costs energy equal to:

(distance from the trap)×(ghost’s stamina)

The total energy used must not exceed a given limit E.

Goal: Determine the maximum number of ghosts you can hunt including the trap ghost, under the above constraints.

Input:
L = 20  
N = 5  
E = 100  

A = [2, 5, 10, 14, 18]  
B = [3, 2, 4, 5, 6]

Output: 3

Explanation:

  • If you trap the ghost at position 10 with stamina 4
  • You can hunt ghosts at positions 14 (stamina 5) and 18 (stamina 6)
    • Cost to hunt at 14: (14 - 10) * 5 = 20
    • Cost to hunt at 18: (18 - 10) * 6 = 48
    • Total cost: 20 + 48 = 68 ≤ 100
  • Total ghosts hunted = 3 (at positions 10, 14, 18)

Interview Questions (1)

Q1
Haunted Road Ghost Hunting (Greedy + Two-Pointer + Sorting)
Data Structures & Algorithms

You're on a haunted straight road of length L with N ghosts positioned along it.

Each ghost is described by:

A position A[i] on the road (0 ≤ A[i] ≤ L)

A stamina level B[i]

You are allowed to trap exactly one ghost, and this ghost becomes your trap ghost. Starting from this position, you can move only to the right, hunting more ghosts under the following rules:

Rules for hunting: You may only hunt ghosts whose stamina is greater than or equal to the trap ghost's stamina.

Hunting a ghost costs energy equal to:

(distance from the trap)×(ghost’s stamina)

The total energy used must not exceed a given limit E.

Goal: Determine the maximum number of ghosts you can hunt including the trap ghost, under the above constraints.

Input:
L = 20  
N = 5  
E = 100  

A = [2, 5, 10, 14, 18]  
B = [3, 2, 4, 5, 6]

Output: 3

Explanation:

  • If you trap the ghost at position 10 with stamina 4
  • You can hunt ghosts at positions 14 (stamina 5) and 18 (stamina 6)
    • Cost to hunt at 14: (14 - 10) * 5 = 20
    • Cost to hunt at 18: (18 - 10) * 6 = 48
    • Total cost: 20 + 48 = 68 ≤ 100
  • Total ghosts hunted = 3 (at positions 10, 14, 18)
Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!