> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/interview_questions_4/sparse-search.md).

# Sparse Search

**10.5 Sparse Search:** Given a sorted array of strings that is interspersed with empty strings, write a method to find the location of a given string.

```
EXAMPLE
Input: ball, {"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""}
Output: 4
```

* Worst case order O(n), Avg. O(logN), O(N) space
* Algorithm: Just ignore the empty strings. If you find an empty string, then preform a radial search outwards from that location until you find a non-empty string. This works because all be need is the closest thing to compare the target string with the middle. Technically, either the right or left string from the space could work, because either one will notify us whether we need to search right or left. We just chose the faster alternative.

```cpp
int binary_search(vector<string> &strs, int low, int high, string target) {
    if (high < low) return -1;

    int middle = low + ((high - low) / 2);

    if (strs.at(middle).empty()) {
        int left = middle - 1;
        int right = middle + 1;

        while (left > low && right < high) {
            if (strs.at(left).empty())
                left--;
            else {
                middle = left;
                break;
            }
            if (strs.at(right).empty())
                right++;
            else {
                middle = right;
                break;
            }
        }
    }

    if (strs.at(middle) == target) return middle;
    // search right
    else if (strs.at(middle) < target) {
        binary_search(strs, middle + 1, high, target);
    }
    else {
        binary_search(strs, low, middle - 1, target);
    }
}

int sparse_search(vector<string> &strs, string str) {
    if (strs.empty() || str == "") return -1;
    return binary_search(strs, 0, strs.size() - 1, str);
}


int main() {
    vector<string> strings = { "at", "", "", "", "ball", "", "", "car", "", "", "dad", "", "" };
    cout << sparse_search(strings, "ball") << endl;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/coding_practice_questions/interview_questions_4/sparse-search.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
