## Sap Labs Interview Question for Senior Software Development Engineers

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
0
of 0 vote

It seems like you have provided a coding problem related to counting k-Spikes in a given array of stock prices. You want to find the number of elements that satisfy specific conditions related to their neighboring elements. Here's a possible approach to solving this problem using Python:

```python
def count_k_spikes(prices, k):
n = len(prices)
count = 0

for i in range(n):
left_count = 0
right_count = 0

# Count elements to the left of prices[i] that are less than prices[i]
for j in range(i - 1, -1, -1):
if prices[j] < prices[i]:
left_count += 1
if left_count >= k:
break

# Count elements to the right of prices[i] that are less than prices[i]
for j in range(i + 1, n):
if prices[j] < prices[i]:
right_count += 1
if right_count >= k:
break

# Check if prices[i] is a k-Spike
if left_count >= k and right_count >= k:
count += 1

return count

# Example usage
prices = [1, 2, 8, 5, 3, 4]
k = 2
result = count_k_spikes(prices, k)
print("Output is:", result) # Output is: 2
```

This code defines a function `count_k_spikes` that takes an array of prices and the value of k as input. It then iterates through each element of the array, counting the number of elements to the left and right that are less than the current element. If both counts are greater than or equal to k, then the current element is considered a k-Spike, and the count is incremented. Finally, the function returns the total count of k-Spikes in the array.

Comment hidden because of low score. Click to expand.
0
of 0 vote

It seems like you have provided a coding problem related to counting k-Spikes in a given array of stock prices. You want to find the number of elements that satisfy specific conditions related to their neighboring elements. Here's a possible approach to solving this problem using Python:

```python
def count_k_spikes(prices, k):
n = len(prices)
count = 0

for i in range(n):
left_count = 0
right_count = 0

# Count elements to the left of prices[i] that are less than prices[i]
for j in range(i - 1, -1, -1):
if prices[j] < prices[i]:
left_count += 1
if left_count >= k:
break

# Count elements to the right of prices[i] that are less than prices[i]
for j in range(i + 1, n):
if prices[j] < prices[i]:
right_count += 1
if right_count >= k:
break

# Check if prices[i] is a k-Spike
if left_count >= k and right_count >= k:
count += 1

return count

# Example usage
prices = [1, 2, 8, 5, 3, 4]
k = 2
result = count_k_spikes(prices, k)
print("Output is:", result) # Output is: 2
```

This code defines a function `count_k_spikes` that takes an array of prices and the value of k as input. It then iterates through each element of the array, counting the number of elements to the left and right that are less than the current element. If both counts are greater than or equal to k, then the current element is considered a k-Spike, and the count is incremented. Finally, the function returns the total count of k-Spikes in the array.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.