Solve three coding interview problems

Quick Overview

This set evaluates skills in string parsing and nested-structure decoding, optimization and stateful decision-making in one-dimensional arrays, and grid-based graph traversal for counting connected components.

Solve three coding interview problems

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given three independent coding questions. ## 1) Decode an encoded string Given an encoded string `s`, decode it using the following rule: - Patterns of the form `k[encoded_string]` mean that `encoded_string` is repeated exactly `k` times. - `k` is a positive integer and may have multiple digits. - Encoded strings may be nested. **Example**: `s = "3[a2[c]]"` should decode to `"accaccacc"`. **Task**: Return the decoded string. **Assumptions/constraints** (you may state any reasonable ones in your solution): - `s` contains only digits, letters, and brackets `[` `]`. - The input is valid (balanced brackets, well-formed encoding). --- ## 2) Max profit with jump decisions Given a 1D array `arr` of integers and starting at index `0`, you can make a decision at each index `i`: - **Take**: gain profit `arr[i]`, then jump to index `i + 1 + arr[i]`. - **Skip**: gain profit `0`, then move to index `i + 1`. You may stop when your index is out of bounds (i.e., `i >= n`). **Task**: Compute the maximum total profit achievable. ### Follow-up How does your approach change if `arr` can contain **negative numbers**? - If you **take** at index `i`, you still gain `arr[i]`, and you still move to index `i + 1 + arr[i]` (which could move backward if `arr[i] < 0`). - Clarify any assumptions needed to ensure termination and handle potential cycles. --- ## 3) Count connected land components in a grid Given an `m x n` grid of characters `'1'` and `'0'`: - `'1'` represents land - `'0'` represents water Two land cells are connected if they are adjacent **horizontally or vertically** (4-directional adjacency). **Task**: Return the number of connected components of land (i.e., the number of “islands”). **Example**: ``` 1 1 0 0 1 0 1 0 1 ``` Answer: `3`.

Overview: This set evaluates skills in string parsing and nested-structure decoding, optimization and stateful decision-making in one-dimensional arrays, and grid-based graph traversal for counting connected components.

Community answers

Answer by mojahidislam221

package code; import java.util.Arrays; public class Solution1 { int index =0; public String decode(String s){ index =0; return decodes(s); } public String decodes(String s) { StringBuilder sb = new StringBuilder(); while(index0){ sb.append(temp); num--; } } else if(s.charAt(index)==']'){ index++; return sb.toString(); }else{ sb.append(s.charAt(index)); index++; } } return sb.toString(); } public static void main(String[] args) { System.out.println((new Solution1()).decode("abc")); } } public class Solution1 { public long profit(int[] arr) { int n = arr.length; if (n == 0) return 0L; long[] dp = new long[n + 1]; // dp[n] = "out of bounds" sink Arrays.fill(dp, Long.MIN_VALUE); dp[0] = 0L; for (int i = 0; i < n; i++) { if (dp[i] == Long.MIN_VALUE) continue; // unreachable // skip: i -> i+1 (i+1 <= n, always in range) dp[i + 1] = Math.max(dp[i + 1], dp[i]); // take: compute landing in long so i + 1 + arr[i] can't wrap long land = (long) i + 1 + arr[i]; int j = (int) Math.min(n, land); // clamp exits to sink dp[j] = Math.max(dp[j], dp[i] + arr[i]); // long + int -> long } return dp[n]; } }
Google logo
Google
Oct 9, 2025
mediumSoftware EngineerOnsiteCoding & Algorithms
28
0

You are given three independent coding questions.

1) Decode an encoded string

Given an encoded string s, decode it using the following rule:

  • Patterns of the form k[encoded_string] mean that encoded_string is repeated exactly k times.
  • k is a positive integer and may have multiple digits.
  • Encoded strings may be nested.

Example: s = "3[a2[c]]" should decode to "accaccacc".

Task: Return the decoded string.

Assumptions/constraints (you may state any reasonable ones in your solution):

  • s contains only digits, letters, and brackets [ ] .
  • The input is valid (balanced brackets, well-formed encoding).

2) Max profit with jump decisions

Given a 1D array arr of integers and starting at index 0, you can make a decision at each index i:

  • Take : gain profit arr[i] , then jump to index i + 1 + arr[i] .
  • Skip : gain profit 0 , then move to index i + 1 .

You may stop when your index is out of bounds (i.e., i >= n).

Task: Compute the maximum total profit achievable.

Follow-up

How does your approach change if arr can contain negative numbers?

  • If you take at index i , you still gain arr[i] , and you still move to index i + 1 + arr[i] (which could move backward if arr[i] < 0 ).
  • Clarify any assumptions needed to ensure termination and handle potential cycles.

3) Count connected land components in a grid

Given an m x n grid of characters '1' and '0':

  • '1' represents land
  • '0' represents water

Two land cells are connected if they are adjacent horizontally or vertically (4-directional adjacency).

Task: Return the number of connected components of land (i.e., the number of “islands”).

Example:

1 1 0
0 1 0
1 0 1

Answer: 3.